【CF 1025C】Plasticine zebra
C. 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.
Input
The only line contains a string ss (1≤|s|≤1051≤|s|≤105, where |s||s| denotes the length of the string ss) comprised of lowercase English letters 'b' and 'w' only, where 'w' denotes a white piece and 'b' denotes a black piece.
Output
Print a single integer — the maximum possible zebra length.
Examples
input
bwwwbwwbw
output
5
input
bwwbwwb
output
3
题意:一个长度为n的字符串,切断任意位置重新相连,求最大相邻字符不同序列。
题目中给出将字符串转置之后得到目标序列,实际上只需要复制一个子串首尾相连遍历一遍找最大不相同序列即可。
代码:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cstdio>
#include<cmath>
#include<set>
#include<map>
using namespace std;
#define inf 0x3f3f3f3f
#define ll long long
#define mem(a,b) memset(a,b,sizeof(a))
#define closeio std::ios::sync_with_stdio(false)
char s[200005];
int main()
{
int n,i,j,ans;
cin>>s+1;
n=strlen(s+1);
for(i=1;i<=n;i++)
s[n+i]=s[i];
for(i=1;i<=n*2;i=j)
{
for(j=i+1;j<=n*2;j++)
if(s[j]==s[j-1])
break;
ans=max(ans,j-i);
}
cout<<min(ans,n)<<endl; //字符串最大不能超过n
return 0;
}