Manacher || Luogu P3805【模板】manacher算法
代码:
1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #define max(a,b) ((a)>(b)?(a):(b)) 5 #define min(a,b) ((a)<(b)?(a):(b)) 6 using namespace std; 7 const int maxn=11000000+50; 8 int len,N,mr,mid,RL[maxn<<1],ans=1; 9 char S[maxn<<1],a[maxn]; 10 inline void Manacher(){ 11 mr=mid=0; 12 for(int i=1;i<=len;i++){ 13 if(i<mr) RL[i]=min(RL[(mid<<1)-i],mr-i); 14 else RL[i]=1; 15 while(i-RL[i]>=1&&i+RL[i]<=len&& 16 S[i-RL[i]]==S[i+RL[i]])RL[i]++; 17 if(i+RL[i]-1>mr){ 18 mr=i+RL[i]-1; 19 mid=i; 20 } 21 ans=max(ans,RL[i]-1); 22 } 23 return; 24 } 25 int main(){ 26 scanf("%s",a); 27 N=strlen(a); 28 S[0]='$'; 29 S[len=1]='#'; 30 for(int i=0;i<N;i++){ 31 S[++len]=a[i]; 32 S[++len]='#'; 33 } 34 Manacher(); 35 printf("%d\n",ans); 36 return 0; 37 }
By:AlenaNuna