Title Address: HDU 3068
A detailed description of the algorithm: Manacher algorithm
#include <stdio.h>#include <math.h>#include <string.h>#include <stdlib.h>#include <iostream>#include <sstream>#include <algorithm>#include <set>#include <queue>#include <stack>#include <map>#pragma COMMENT (linker, "/stack:102400000,102400000")using namespace STD;typedef Long LongLL;Const intinf=0x3f3f3f3f;Const DoublePi=ACOs(-1.0);Const Doubleesp=1e-6;Const intmaxn=110010;CharSTR[MAXN];//Raw stringChars[maxn*2];//String after Manacher processingintp[maxn*2];//Save palindrome radius, the length of each palindrome string must be an odd numberintNvoidManacher () {intI for(i=1; str[i]!=' + '; i++) {s[i*2]=str[i]; s[i*2+1]=' # '; } s[0]=' $ '; s[1]=' # '; n=i*2+2; s[n]=' + ';//Control End intMaxl,id; Id=maxl=0;memset(P,0,sizeof(p)); for(intI=1; i<n; i++) {if(p[id]+id>i) P[i]=min (p[2*ID-I],P[ID]+ID-I);Elsep[i]=1; while(S[i+p[i]]==s[i-p[i]]) p[i]++;if(P[i]+i>p[id]+id) {id=i; }if(P[I]>MAXL) Maxl=p[i]; }printf("%d\n", maxl-1);}intMain () { while(~scanf('%s ', &str[1]) {manacher (); }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 3068-Longest palindrome (Manacher algorithm O (n) to find the longest palindrome string)