标签:
HDU 3068 Manacher裸题
1 #include <cstdio> 2 #include <cstring> 3 const int Maxn=110100; 4 char Str[Maxn<<1],STR[Maxn<<1]; 5 int Len,x,P[Maxn<<1],Id,Mx; 6 inline int Max(int x,int y) {return x>y?x:y;} 7 inline int Min(int x,int y) {return x>y?y:x;} 8 inline Init() 9 { 10 Len=strlen(Str+1); 11 for (int i=1;i<=Len;i++) STR[i]=Str[i]; 12 Str[0]=‘$‘; Str[1]=‘#‘; 13 for (int i=1;i<=Len;i++) Str[i<<1]=STR[i],Str[i<<1|1]=‘#‘; 14 Len=Len<<1|1; Id=1;Mx=0; 15 } 16 inline int Manacher() 17 { 18 for (int i=2;i<=Len;i++) 19 { 20 if (Mx>i) P[i]=Min(P[2*Id-i],Mx-i); else P[i]=1; 21 while (Str[i+P[i]]==Str[i-P[i]]) P[i]++; 22 if (i+P[i]>Mx) Mx=P[i]+i,Id=i; 23 } 24 int Ret=0; 25 for (int i=1;i<=Len;i++) Ret=Max(Ret,P[i]); 26 return Ret-1; 27 } 28 int main() 29 { 30 while (scanf("%s",Str+1)!=EOF) 31 { 32 Init(); 33 printf("%d\n",Manacher()); 34 } 35 return 0; 36 }
标签:
原文地址:http://www.cnblogs.com/yyjxx2010xyu/p/5583411.html