Title: http://acm.hdu.edu.cn/showproblem.php?pid=5421
Because to be in front of the character, so maintain a prefix chain and suffix chain, on the same palindrome tree, if there is a longest palindrome suffix (or prefix) length of the total, then let the prefix (or suffix) of the last also assigned to the current node.
#include <cstring>#include<iostream>#include<algorithm>#include<cstdio>#defineRep (i,l,r) for (int i=l;i<=r;i++)#defineDown (i,l,r) for (int i=l;i>=r;i--)#defineCLR (x, y) memset (x,y,sizeof (×))#definell Long Long#defineMAXN 200500using namespacestd;intN,op;CharC;intRead () {intx=0, f=1;CharCh=GetChar (); while(!isdigit (CH)) {if(ch=='-') f=-1; Ch=GetChar ();} while(IsDigit (CH)) {x=x*Ten+ch-'0'; Ch=GetChar ();} returnx*F;}structdata{ll sum; intl,r,tot,fail[maxn],l[maxn],nxt[maxn][ -],s[maxn],last[2],CNT[MAXN]; intNewNodeintLen) {CLR (Nxt[tot],0); L[tot]=len; cnt[tot]=0; returntot++; } voidinit () {tot=0; sum=0; NewNode (0); NewNode (-1); fail[0]=1; last[0]=last[1]=1; CLR (S,-1); L=n; r=n-1; } intGetfail (intOpintv) { if(OP) while(s[r-l[v]-1]!=S[R]) v=Fail[v]; Else while(s[l+l[v]+1]!=S[L]) v=Fail[v]; returnv; } voidAddintOpintc) { if(OP) s[++r]=c;Elses[--l]=C; intCur=Getfail (Op,last[op]); if(!Nxt[cur][c]) { intNow=newnode (l[cur]+2); Fail[now]=Nxt[getfail (Op,fail[cur])][c]; NXT[CUR][C]=Now ; Cnt[now]=cnt[fail[now]]+1; } Last[op]=Nxt[cur][c]; if(l[last[op]]==r-l+1) last[op^1]=Last[op]; Sum+=Cnt[last[op]]; }}t;intMain () {//freopen ("In.txt", "R", stdin); while(~SCANF ("%d",&N)) {T.init (); Rep (I,1, N) {op=read (); if(op<=2) {C=GetChar (); T.add (OP-1, C-'a'); } Else if(op==3) printf ("%d\n", t.tot-2); Elseprintf"%lld\n", t.sum); } } return 0;}
Hdu-5421victor and String