Topic links
Earlier did not write this article, fill it up.
Record current ex[] The largest palindrome center ID and the farthest extension range mx!
About the structure of a string:
Should be @ #A#B#C#B#A# $
, rather than@ A#B#C#B#A $
For example @a#b#b#c$
, the answer should be \ (max\{ex[i]\}-1\), and the second in many cases the answer is \ (max\{ex[i]\}\).
The optimal solution does not change the sequence of the odd couple to discuss the feeling SXBK. Actually, it's nothing.
#include <cstdio>#include <cstring>#include <algorithm>Const intn=22000010;intN,ex[n];CharTmp[n>>1],s[n];voidInit () {N=strlen (TMP), s[0]='@'; for(intI=0; i<n; ++i) s[i<<1|1]='#', s[(i<<1)+2]=tmp[i]; s[n=2*n+2]='$', S[n-1]='#';}voidManacher () {intres=0, mx=0, id; for(intI=1; i<n; ++i) {if(I<MX) Ex[i]=std::min (ex[id*2-I],MX-I);Elseex[i]=1; while(S[i+ex[i]]==s[i-ex[i]]) ++ex[i];//if (ex[i]>res) res=ex[i];//not in this update is also right, too lazy to specifically think about it. if(I+EX[I]>MX) Id=i,mx=i+ex[i],res=std::max (Res,ex[i]); } printf ("%d"Res-1);}intMain () {scanf ("%s", TMP), Init (), Manacher ();return 0;}
Rokua. 3805. [Template]manacher algorithm