Game with string-题解

题目:https://codeforces.com/problemset/problem/1104/B
A 与 B 正在玩一个关于由小写拉丁字符构成的字符串 s 的游戏,每一个人会轮流操作,先 A 后 B,对于每一次操作,操作者需要将 s中的两个连续且相同的字符消除,消除后的字符串由另一个人操作,同样的,对于每一次操作,如果不能找到两个符合要求的字符,那么操作者输。

例如以下情况:

s = ‘‘xaax’’

首先是 A 操作,他只能将 '‘aa’′ 删除,剩下 ′′xx′′,被 B 消除后字符串为空,A 不能找到符合的字符串,故 A输。
AC代码如下:

Examples
inputCopy
abacaba
outputCopy
No
inputCopy
iiq
outputCopy
Yes
inputCopy
abba
outputCopy
No
Note
In the first example the first player is unable to make a turn, so he loses.

In the second example first player turns the string into "q", then second player is unable to move, so he loses.


#include< iostream>
#include< string>
#include< cstdio>
#include< stack>
using namespace std;
int main()
{
    string s;
    cin>>s;
    stack <int>st;
    st.push(s[0]);
    int ans=0;
    for(unsigned int i=1;i<s.size();i++)
    {
        if(!st.empty())
        {
            if(s[i]== st.top())
            {
                ans++;
                st.pop();
            }
            else
                st.push(s[i]);
        }
        else
            st.push(s[i]);
    }
    if(ans== 0||ans%2==0)
        cout<<"NO"<<endl;
    else
        cout<<"YES"<<endl;
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值