Blue Bridge Cup improve --- necklace

Article Directory

topic

Here Insert Picture Description
Here Insert Picture Description

Thinking

According to said subject, a character string inputted from any of the local cut, c ++ there substr function, the parameter is the starting index and the desired length. Specific introduction can go and see this blog
introduced substr function
With this function we will be able to enter a string of successful off and then re-merged in different places. Just need to follow the rest of the length of the front meaning of the questions to ask after a period of some length and re-adding the line.

Code

#include<iostream>
#include<string.h>
#include<cmath>
using namespace std;
// string s="abccdefgh";
// cout<<s.substr(4)<<endl;
// cout<<s.substr(0,4);
string n;


int get_string(string s,int i){
    string s1=s.substr(i)+s.substr(0,i);
    //cout<<s1<<endl;
    int j1=0,j2=0;
    char c1='w';
    char c2='w';
    for(int k=0;k<s1.length();k++){
        if(s1[k]!=c1){
            c1=s1[k];
            break;
        }
    }

    for(int k=s1.length()-1;k>0;k--){
        if(s1[k]!=c2){
            c2=s1[k];
            break;
        }
    }


    for(int j=0;j<s1.length();j++){
        if(s1[j]==c1||s1[j]=='w')
            j1++;
        else{
            break;
        } 
    }

    for(int j=s1.length()-1;j>0;j--){
        if(s1[j]==c2||s1[j]=='w')
            j2++;
        else{
            break;
        }
    }


    if(j1==s1.length())
        return j1;
    else{
        return j1+j2;
    }
}


int main(){
    cin>>n;
    int M=0;
    for(int i=0;i<n.length();i++){
        int m=get_string(n,i);
        M=M>m?M:m;
    }
    cout<<M<<endl;
    return 0;
}

Here Insert Picture Description
Here Insert Picture Description

Algorithms really more questions on top of the brush, do not stop

Published 85 original articles · won praise 55 · views 20000 +

Guess you like

Origin blog.csdn.net/shelgi/article/details/104190944