B. Game with string 思维问题转化

B. Game with string 思维问题转化

题意

有一个字符串 每次可以删去连续的两个同样的字符,两个人轮流删,问最后谁能赢

思路

初看有点蒙蔽,仔细看看样例就会发现其实就是一个括号匹配问题,直接搞一个栈看有多少对合法的括号即可

#include<bits/stdc++.h>
#define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;i++)
#define MS(arr,arr_value) memset(arr,arr_value,sizeof(arr)) 
#define F first 
#define S second
#define pii pair<int ,int >
#define mkp make_pair
#define pb push_back
using namespace std;
typedef long long ll;
const int maxn = 3e5+4;
char s[maxn];
stack<char>q;
int main(){
    int ans=0;
    cin>>s+1;
    int len=strlen(s+1);
    q.push(s[1]);
    for(int i=2;i<=len;i++){
        if(!q.empty()&&s[i]==q.top()){
            ans++;
            q.pop();
        }
        else q.push(s[i]);
    }
    if(ans%2==1)cout<<"Yes\n";
    else cout<<"No\n";
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/ttttttttrx/p/10800646.html