CF1025C Plasticine zebra

题目描述:

Is there anything better than going to the zoo after a tiresome week at work? No wonder Grisha feels the same while spending the entire weekend accompanied by pretty striped zebras.

Inspired by this adventure and an accidentally found plasticine pack (represented as a sequence of black and white stripes), Grisha now wants to select several consequent (contiguous) pieces of alternating colors to create a zebra. Let's call the number of selected pieces the length of the zebra.

Before assembling the zebra Grisha can make the following operation 00 or more times. He splits the sequence in some place into two parts, then reverses each of them and sticks them together again. For example, if Grisha has pieces in the order "bwbbw" (here 'b' denotes a black strip, and 'w' denotes a white strip), then he can split the sequence as bw|bbw (here the vertical bar represents the cut), reverse both parts and obtain "wbwbb".

Determine the maximum possible length of the zebra that Grisha can produce.

题目大意:

给你一个长度为 \left|s\right|s∣ 的01串 ss,每次操作你可以任选一个 kk ,使01串的 [1,k][1,k] 和 (k,\left|s\right|](k,s] 分别翻转(其中一个区间可以为空),求经过任意次操作后能得到的最长的01交替出现的子串的长度。(实际题目中01用w和b代替)

思路:

有一个特别神奇的点,我们做一次操作,把分界线两端的01串分别翻转,考虑到如果你把整个串翻转对答案没有影响,把整个串翻转我们就会发现,其实相当于把后一个串接到前一个串之前,于是我们只要把01串围成一个环,计算最长的01交替出现的字串长度,即为答案。

以下代码:

#include<bits/stdc++.h>
#define il inline
#define _(d) while(d(isdigit(ch=getchar())))
using namespace std;
const int N=1e5+5;
char s[N];int ans,n;
il int read(){
    int x,f=1;char ch;
    _(!)ch=='-'?f=-1:f;x=ch^48;
    _()x=(x<<1)+(x<<3)+(ch^48);
    return f*x;
}
int main()
{
    scanf( "%s",s+1);
    n=strlen(s+1);int res=1;
    if(n==1){puts("1");return 0;}
    for(int i=2;i<=n;i++){
        if(s[i]==s[i-1]){
            ans=max(ans,res);res=0;
        }
        res++;
    }
    ans=max(ans,res);
    if(s[1]!=s[n]){
        res=1;
        for(int i=2;i<=n;i++){
            if(s[i]==s[i-1])break;
            res++;
        }
        res++;
        for(int i=n-1;i;i--){
            if(s[i]==s[i+1]||res>=n)break;
            res++;
        }
        ans=max(ans,res);
    }
    printf("%d\n",min(ans,n));
    return 0;
}
View Code

 

posted @ 2019-02-11 16:49  Jessiejzy  阅读(352)  评论(0编辑  收藏  举报