Game with string CodeForces - 1104B

虽然只是B题,还是div2的

但感觉挺有意思,所以写一篇博客记录一下

最初的想法是利用DP去做,f[s]=true表示字符串s对应先手赢,否则对应后手赢,大致想了下发现是指数级别的算法,看了下范围直接pass掉

然后就接着想,发现如果两个相等的字符被消掉之后,相等字符的周边靠过来,如果还能消除的话他们就是相等,一想这不就是括号匹配?

然后问题就转化为了寻找合法括号字段的长度

然后就简单了

竟然是用栈来做!!!

#include<cstdio>
#include<map>
#include<set>
#include<queue>
#include<string>
#include<stack>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
//#define endl "\n"
#define inf 0x3f3f3f3f
#define me(a,b) memset(a,b,sizeof(a))

string s;
stack<char> st;
int main()
{
    cin>>s;
    int cnt=0;
    for(auto i:s){
        if(st.empty()) st.push(i);
        else {
            char c=st.top();
            if(c==i) cnt++,st.pop();
            else st.push(i);
        }
    }
    if(cnt&1) puts("Yes");
    else puts("No");

}

猜你喜欢

转载自www.cnblogs.com/033000-/p/10463845.html
今日推荐