Manacher (horse-drawn car) algorithm can find the longest palindrome substring in O (n)
The principle of the algorithm is the use of an ID and MX at the time of matching, and the nature of a palindrome string symmetry, so in the complexity of the calculation, the preceding constants do not seem very large
HDU 3068 manacher Algorithm template problem
#include <bits/stdc++.h>using namespacestd;Const intmaxn=3e5+7;CharS[MAXN],STR[MAXN];intLen1,len2,p[maxn],ans;voidinit () {str[0]='#'; str[1]='#'; for(intI=0; i<len1;i++) {Str[i*2+2]=S[i]; Str[i*2+3]='#'; } len2=len1*2+2; STR[LEN2]='*';}voidManacher () {intId=0, mx=0; for(intI=1; i<len2;i++){ if(mx>i) P[i]=min (p[id*2-i],mx-i); Elsep[i]=1; for(; Str[i+p[i]]==str[i-p[i]];p [i]++); if(p[i]+i>MX) {MX=p[i]+i; ID=i; } } return ;}intMain () { while(~SCANF ("%s", s)) {Len1=strlen (s); Init (); Manacher (); Ans=0; for(intI=1; i<len2;i++) {ans=Max (ans,p[i]); } printf ("%d\n", ans-1); } return 0;}/*Aaaaabab*/
Manacher algorithm O (n) to find the longest palindrome substring in a string hdu 3068 (template title)