Test instructions
Idea: Generate some palindrome to make up the number of segments generated least
There is obviously an optimal scheme to make the resulting palindrome the very long palindrome string of the target string.
To find the longest palindrome string for each position, the problem is transformed into:
The minimum number of segments to cover the entire area given the segments of the known start and end positions
This can be done by bit to find out where the area that is currently covered can be extended farthest.
can also preprocess the prefix minimum value, jump directly call
1 Constoo=1000000000;2 varT,a,x,y,p:Array[1..110000] ofLongint;3 Len,n,i,id,mx,ans,m:longint;4 ch:ansistring;5 6 functionlowbit (x:longint): Longint;7 begin8Exit (x and(-x));9 End;Ten One functionMax (x,y:longint): Longint; A begin - ifX>y Thenexit (x); - exit (y); the End; - - functionmin (x,y:longint): Longint; - begin + ifX<y Thenexit (x); - exit (y); + End; A at procedureUpdate (x,y:longint); - begin - whilex<=n-2 Do - begin -t[x]:=Max (t[x],y); -x:=x+lowbit (x); in End; - End; to + functionquery (x:longint): Longint; - begin thequery:=-Oo; * whileX>0 Do $ beginPanax Notoginsengquery:=Max (query,t[x]); -x:=x-lowbit (x); the End; + End; A the begin +Assign (input,'bzoj3790.in'); Reset (input); -Assign (output,'Bzoj3790.out'); Rewrite (output); $ while notEof Do $ begin - READLN (CH); -len:=Length (CH); the iflen=0 ThenBreak ; -Fillchar (A,sizeof (a),0);WuyiFillchar (P,sizeof (p),0); then:=2; a[1]:= -; a[2]:= -; - fori:=1 toLen Do Wu begin -Inc (N); A[n]:=ord (Ch[i])-ord ('a')+1; AboutInc (N); a[n]:= -; $ End; -Inc (N); a[n]:= in; -mx:=0; id:=1; - fori:=2 toN-1 Do A begin + ifMx>i ThenP[i]:=min (p[id*2-i],mx-i) the Elsep[i]:=1; - whileA[i-p[i]]=a[i+p[i]] DoInc (P[i]); $ ifP[i]+i>mx Then the begin theMx:=p[i]+i; id:=i; the End; the End; - fori:=1 toM Do in begin thex[i]:=0; y[i]:=0; the End; Aboutm:=0; the fori:=2 toN-1 Do the begin theInc (M); X[m]:=i-p[i]; y[m]:=i+p[i]-2; + End; -Fillchar (t,sizeof (t),0); the fori:=1 toM DoUpdate (x[i],y[i]);Bayii:=1; ans:=0; the whilei<n-2 Do the begin -I:=query (i+1); - Inc (ANS); the End; theWriteln (ans-1); the End; the close (input); - close (output); the End.
"BZOJ3790" Magic Necklace (manacher, tree-shaped array)