码迷,mamicode.com
首页 > 其他好文 > 详细

1.23 div2 b-字符串处理(string 与 char 字符串

时间:2019-01-23 14:33:59      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:ati   clu   ons   let   cstring   ios   位置   ring   and   

B. Game with string
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Two people are playing a game with a string s

, consisting of lowercase latin letters.

On a players 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 s
, consisting of lowercase latin letters (1≤|s|≤100000), where |s| means the length of a string s

.
Output

If the first player wins, print "Yes". If the second player wins, print "No".

开始用字符串模拟判断删除的整个过程,结果各种分类讨论,各种出错

而用string里自带的 erase删除字符 瞬间简洁

ps:c++ string的erase删除方法

1. 从位置pos=10处开始删除,直到结尾  str.erase(10);

2.  删除迭代器[first, last)区间的所有字符,返回一个指向被删除的最后一个元素的下一个字符的迭代器. str.erase(str.begin()+10);

string.size() 不能判断空字符串 删除字符前要用len保存字符长度

#include<cstdio>
#include<cstring>
#include<iostream>
#include<string>
using namespace std;
 int main(){
     string s;
     int k=0;
     getline(cin,s);
     int len=s.size();
     for(int i=0;i<len;i++){
         while(i>=0&&s[i]==s[i+1]){
//             printf("%d %d %c %c\n",i,i+1,s[i],s[i+1]);
             k++;
             s.erase(s.begin()+i,s.begin()+i+2);
             len-=2;
              if(len<=1)break;
             i--;
            if(i<0)i=0;
//             printf("%d %d %c %c\n",i,i+1,s[i],s[i+1]);
         }
         if(len<=1)break;
     }
     if(k%2==0)printf("No");
     else printf("Yes");
     return 0;
 }

 

1.23 div2 b-字符串处理(string 与 char 字符串

标签:ati   clu   ons   let   cstring   ios   位置   ring   and   

原文地址:https://www.cnblogs.com/-ifrush/p/10308643.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!