Codeforces Round #534 (Div. 2) B. Game with string(stack具有纪念意义的一题)

版权声明:欢迎转载,若转载,请标明出处,如有错误,请指点,也欢迎大佬们给出优化方法 https://blog.csdn.net/Charles_Zaqdt/article/details/86616594

题目链接:http://codeforces.com/contest/1104/problem/B

       题意是给了一个字符串,有两个人轮流操作,每个人删除任意位置两个相同的相邻的字符,删除后剩下的字符串再拼起来,直到不能再操作了为止,如果是第二个人不能再操作了就输出Yes,否则就是No。

       不是很难,一读懂题就想到了括号配对,所以就有了历史最快的过题记录!(差点拿一血)


AC代码:

#include <bits/stdc++.h>
using namespace std;
int pre[100005];

int main()
{
  string str;
  cin>>str;
  int len = str.length();
  stack<char> s;
  int ans = 0;
  for(int i=0;i<len;i++){
    if(s.size() == 0){s.push(str[i]);continue;}
    if(s.top() == str[i]){
      s.pop();
      ans++;
    }
    else{
      s.push(str[i]);
    }
  }
  if(ans % 2 == 1)puts("Yes");
  else puts("No");
  return 0;
}

猜你喜欢

转载自blog.csdn.net/Charles_Zaqdt/article/details/86616594