1. 程式人生 > >1.23 div2 b-字符串處理(string 與 char 字符串

1.23 div2 b-字符串處理(string 與 char 字符串

amp -c 方法 ble ask else ast 出錯 ons

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 字符串