https://vjudge.net/contest/356507#problem/C
00001111
在前i-1个全相等的情况下,对于位置i,如果a[i]!=a[i-1],要么把前i-1个反转,要么把n-(i-1)位置的反转。按位处理,每次从这两个情况里面取最大值。最后从这些最大值里取最小值就是答案
#include <iostream> #include <cstdio> #include <queue> #include <algorithm> #include <cmath> #include <cstring> #define inf 2147483647 #define N 1000010 #define p(a) putchar(a) #define For(i,a,b) for(int i=a;i<=b;++i) using namespace std; int n,cnt,ans; int a[N]; char c[N]; void in(int &x){ int y=1;char c=getchar();x=0; while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();} while(c<='9'&&c>='0'){ x=(x<<1)+(x<<3)+c-'0';c=getchar();} x*=y; } void o(int x){ if(x<0){p('-');x=-x;} if(x>9)o(x/10); p(x%10+'0'); } signed main(){ cin>>(c+1); n=(int)strlen(c+1); For(i,2,n){ if(c[i]!=c[i-1]) a[++cnt]=max(i-1,n-(i-1)); } ans=n; For(i,1,cnt) ans=min(ans,a[i]); o(ans); return 0; }