您的位置:首页 > 编程语言 > C语言/C++

C++_USACO_求一个字符串的从开头字母开始,直到发现某一不相同字母时,这个子串的长度

2013-07-17 20:09 351 查看
#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也是可以视为任何字符。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐