codeforces 1025C Plasticine zebra

链接http://codeforces.com/contest/1025/problem/C

在由w和b组成的字符串中切一刀,两段各自逆序,然和拼接,要求最后w和b轮流出现的最长子段的长度。这个操作有一个很特殊的性质。先考虑字符串“1234|54321”在‘|’处切开,然后逆序之后的新字符串是“4321|12345”。想象将字符串的两段连起来组成一个环,可以发现字符串的相对顺序没变,原字符串顺时针和后字符串的逆时针是一样的,所以只要在字符串中找到最长的符合要求的子段即可。

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#define DEBUG(x) cout<<#x<<" = "<<x<<endl
using namespace std;
const int MAXN=1e5+10;
char s[MAXN];
int main()
{
//    freopen("in.txt","r",stdin);
    scanf("%s",s);
    int l=strlen(s);
    bool w=s[0]=='w';
    int ans=-1;
    int cnt=0;
    int k=0;
    while(1){
        int i=k%l;
        if(w^(s[i]=='w')){
            ans=max(ans,cnt);
            if(k>=l)break;
            cnt=0;
            w=s[i]=='w';
            ///if(s[i]=='b')k++;
        }
        else {
            cnt++;
            w^=1;
            k++;
        }
        if(k==2*l){ans=cnt/2;break;}
    }
    printf("%d\n",ans);
}

 

posted @ 2018-08-26 11:08  MalcolmMeng  阅读(203)  评论(0编辑  收藏  举报