LA 4975
回文串的题,求最大的双重回文串;
重新复习了一下manacher算法;
代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #define M 310010 5 using namespace std; 6 char b[M],a[M<<1]; 7 int p[M<<1]; 8 int main() 9 { 10 int t,maxid,maxl,i,n,id; 11 scanf("%d",&t); 12 while(t--) 13 { 14 scanf("%s",b+1); 15 for(i=1;b[i]!='\0';i++) 16 if(b[i]<='Z'&&b[i]>='A') 17 b[i]=b[i]-'A'+'a'; 18 memset(a,0,sizeof a); 19 n=0; 20 a[n++]='?'; 21 a[n++]='#'; 22 for(i=1; b[i]!='\0'; i++) 23 { 24 a[n++]=b[i]; 25 a[n++]='#'; 26 } 27 a[n]=0; 28 maxid=maxl=0; 29 for(i=1; i<n; i++) 30 { 31 if(maxid>i)p[i]=min(p[2*id-i],maxid-i); 32 else p[i]=1; 33 while(a[i+p[i]]==a[i-p[i]])p[i]++; 34 if(p[i]+i>maxid) 35 { 36 maxid=p[i]+i; 37 id=i; 38 } 39 } 40 for(i=1;i<n;i++)p[i]--; 41 for(i=1;i<n;i+=2) 42 { 43 int cur=p[i]; 44 cur=cur/4*4; 45 for (;cur>maxl; cur-=4) 46 { 47 if (p[i+cur/2]>=cur/2&&p[i-cur/2]>=cur/2) 48 { 49 maxl=cur; 50 break; 51 } 52 } 53 } 54 printf("%d\n",maxl); 55 } 56 return 0; 57 }