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;
     }
  • 相关阅读:
    linux 如何显示一个文件的某几行(中间几行)
    Cookie——Javascript
    CSS——4种定位
    Javascript——DOM
    javascript之八——BOM
    Javascript——闭包、作用域链
    Struct2
    javaweb——Servlet作为控制器
    排序算法——快排思想
    java——获取从控制台输入的数据的方法
  • 原文地址:https://www.cnblogs.com/-ifrush/p/10308643.html
Copyright © 2011-2022 走看看