Game with string CodeForce#1104B 栈、串

题目链接:Game with string

题目原文

Two people are playing a game with a string ?s, 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.

思路

很显然,假设可以删掉的'xx'的个数是奇数则先手玩家胜,若为偶数则后手玩家胜。

所以问题转化为求解s中可以删掉的xx'的个数。如果用模拟的话,估计会超时。遇到这种部分回文的序列,用栈来处理就方便很多了。把字符串s里的字符依次入栈,如果s[i]和栈顶元素相同,就出栈,同时ans++,最后判断ans的奇偶性即可。

题解

 1 #include <iostream>
 2 #include <cstring>
 3 #include <stack>
 4 
 5 using namespace std;
 6 
 7 stack<char> s;
 8 string str;
 9 
10 int main(int argc, char const *argv[])
11 {
12     cin >> str;
13     int ans = 0;
14     for(int i = 0; i < str.length(); i++)
15     {
16         if(!s.empty() && str[i] == s.top())
17         {
18             ans++;
19             s.pop();
20         }
21         else
22         {
23             s.push(str[i]);
24         }
25     }
26     if(ans % 2 == 0)
27     {
28         cout << "No" << endl;
29     }
30     else
31     {
32         cout << "Yes" << endl;
33     }
34     return 0;
35 }

 

转载于:https://www.cnblogs.com/SaltyFishQF/p/10310463.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值