C++_USACO_求一个字符串的连续相同字符的最大长度

#include<iostream>
using namespace std;
int maxLen(string newstr){
    int i=0;
    int max_len=0;
    int n=newstr.size();
    while(i<n){
        int temp_len=0;
        char br=' ';
        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;
        }
        if(max_len<temp_len){
            max_len=temp_len;
        }    
    }
    return max_len;
}
int main(){
    string str="rrrrrbrrbbrbbbbrrrrbbrbrrrbbb";
    int n;
    n=maxLen(str);
    cout<<n<<endl;
return 0;
}

 

posted @ 2013-07-15 11:34  开心成长  阅读(350)  评论(0编辑  收藏  举报