zoukankan      html  css  js  c++  java
  • 1.23 div2 b-字符串处理(string 与 char 字符串

    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 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 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
    ",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
    ",i,i+1,s[i],s[i+1]);
             }
             if(len<=1)break;
         }
         if(k%2==0)printf("No");
         else printf("Yes");
         return 0;
     }
  • 相关阅读:
    java程序员究竟应该掌握点什么
    Java程序员集合框架面试题
    数组名和数组名取地址的区别
    二维、三维数组转一维数组
    函数指针 行指针 指针数组
    转:如何成为一个优秀的程序员
    转:最小堆的数组实现
    for_each使用方法详解[转]
    c++虚函数的作用是什么?
    转:C语言 可变参数
  • 原文地址:https://www.cnblogs.com/-ifrush/p/10308643.html
Copyright © 2011-2022 走看看