http://poj.org/problem?id=3974
Template problem, the Manacher algorithm mainly utilizes the symmetry of the matched palindrome string, and makes use of the previously matched palindrome string to make the time complexity change from O (n^2) to O (n).
Https://www.cnblogs.com/xiaoningmeng/p/5861154.html detailed explanation
https://www.zhihu.com/question/30226229 This is the interpretation of the complexity O (n)
Code
1#include <cstdio>2#include <cstring>3#include <algorithm>4#include <cmath>5#include <iostream>6#include <map>7#include <ctime>8 using namespacestd;9 Const intmaxn=1000010;Ten CharC[MAXN]; One Chars[maxn*2]; A intp[maxn*2]; - intLen; - voidinit () { thelen=strlen (c); - intj=1; -s[0]='$'; s[1]='#'; - for(intI=0; i<len;i++){ +s[++j]=C[i]; -s[++j]='#'; +}s[++j]=' /'; Alen=J; at } - intManacher () { -Init ();intans=1; - intid,mx=0; - for(intI=1; i<len;i++){ - if(I<MX) P[i]=min (p[2*id-i],mx-i); in Elsep[i]=1; - while(s[i-p[i]]==s[i+P[i]]) top[i]++; + if(mx<i+P[i]) { -Id=i; themx=i+P[i]; * } $ans=Max (ans,p[i]);Panax Notoginseng } - returnans-1; the } + intMain () { A for(intI=1;; ++i) { thescanf"%s",&c); + if(c[0]=='E') Break; -printf"Case %d:%d\n", I,manacher ()); $ } $ return 0; -}
View Code
POJ 3974 palindrome string manacher algorithm