题意:
n<=1e5
思路:可以证明答案即为极长交替出现的串长度之和
需要注意其实这个串是一个环,复制后再做
1 #include<cstdio> 2 #include<cstring> 3 #include<string> 4 #include<cmath> 5 #include<iostream> 6 #include<algorithm> 7 #include<map> 8 #include<set> 9 #include<queue> 10 #include<vector> 11 using namespace std; 12 typedef long long ll; 13 typedef unsigned int uint; 14 typedef unsigned long long ull; 15 typedef pair<int,int> PII; 16 typedef vector<int> VI; 17 #define fi first 18 #define se second 19 #define MP make_pair 20 #define N 2100000 21 #define MOD 1000000007 22 #define eps 1e-8 23 #define pi acos(-1) 24 #define oo 110000000000000 25 26 27 char a[N],b[N]; 28 29 30 int read() 31 { 32 int v=0,f=1; 33 char c=getchar(); 34 while(c<48||57<c) {if(c=='-') f=-1; c=getchar();} 35 while(48<=c&&c<=57) v=(v<<3)+v+v+c-48,c=getchar(); 36 return v*f; 37 } 38 39 40 void swap(int &x,int &y) 41 { 42 int t=x;x=y;y=t; 43 } 44 45 46 ull gcd(ull a, ull b) 47 { 48 return a == 0 ? b : gcd(b % a, a); 49 } 50 51 52 int main() 53 { 54 // freopen("1.in","r",stdin); 55 // freopen("1.out","w",stdout); 56 scanf("%s",a); 57 int n=strlen(a); 58 for(int i=1;i<=n;i++) b[i]=a[i-1]; 59 for(int i=n+1;i<=n*2-1;i++) b[i]=b[i-n]; 60 int m=n*2-1; 61 int t=0; 62 int w=1; 63 int ans=1; 64 while(t<n) 65 { 66 t++; 67 if(w<t) w=t; 68 int tmp=1; 69 while(w+1<=m&&w+1<=t+n-1&&b[w]!=b[w+1]) 70 { 71 w++; tmp++; 72 } 73 // printf("%d %d ",t,w); 74 ans=max(ans,tmp); 75 } 76 // for(int i=1;i<=m;i++) printf("%c",b[i]); 77 printf("%d ",ans); 78 return 0; 79 }