Two people are playing a game with a string ss, consisting of lowercase latin letters.
On a player's turn, he should choose two consecutive equal letters in the string and delete them.
For example, if the string is equal to "xaax" than there is only one possible turn: delete "aa", so the string will become "xx". A player not able to make a turn loses.
Your task is to determine which player will win if both play optimally.
Input
The only line contains the string ss, consisting of lowercase latin letters (1≤|s|≤1000001≤|s|≤100000), where |s||s| means the length of a string ss.
Output
If the first player wins, print "Yes". If the second player wins, print "No".
Examples
input
Copy
abacaba
output
Copy
No
input
Copy
iiq
output
Copy
Yes
input
Copy
abba
output
Copy
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 <bits/stdc++.h>
using namespace std;
const int MAXN = 100010;
vector<char> ss;
int main() {
string ch;
cin >> ch;
ss.push_back(ch[0]);
int k = 1;
int cnt = 0;
for(int i = 1; i < ch.size(); i++){
if(ch[i] == ss[k - 1] && !ss.empty()){
ss.erase(ss.begin() + k - 1);
k--;
cnt += 2;
}else{
ss.push_back(ch[i]);
k++;
}
}
//cout << ss.size() << " " << ch.size();
if(ss.size() == ch.size()) cout << "No" << endl;
else{
if(ss.empty()){
if((cnt / 2) & 1) cout << "Yes" << endl;
else cout << "No" << endl;
}else{
if((cnt / 2) & 1) cout << "Yes" << endl;
else cout << "No" << endl;
}
}
return 0;
}

本文介绍了一种基于字符串匹配的游戏策略解析方法,通过使用栈结构和计数器,有效地判断了游戏中哪位玩家将在最优策略下获胜。文章详细阐述了算法实现过程,包括输入字符串的处理、连续相同字符的删除逻辑以及最终胜利者的判断依据。
1173

被折叠的 条评论
为什么被折叠?



