C++_USACO_求一个字符串的从开头字母开始,直到发现某一不相同字母时,这个子串的长度
#include<iostream> using namespace std; int first_maxLen(string newstr){ int i=0; int n=newstr.size(); int temp_len=0; char br=' '; if(newstr[0]=='w'){ while(i<n){ if(newstr[i]=='w' || newstr[i]==br){ temp_len++; i++; continue; } if(br==' '){ br=newstr[i]; temp_len++; i++; continue; } if(br!=newstr[i]) break; } } else{ br=newstr[0]; while(i<n){ if(newstr[i]==br||newstr[i]=='w'){ temp_len++; i++; continue; } else break; } } return temp_len; } int main(){ string str="wwwbbrwrbrbrrbrbrwrwwrbwrwrrb"; int n; n=first_maxLen(str); cout<<n<<endl; return 0; }
与前一篇的maxLen()不同。w也是可以视为任何字符。