Total code time: 3h
A very simple problem, as long as the Manacher algorithm is aware of the nature (the number of different palindrome string is O (n)), with the suffix automaton or suffix array can be easily resolved.
But this problem tuned for a long time, a lot of waste, one is because the suffix automaton template is not proficient, but the manacher algorithm flow does not have a clear understanding.
Write code with a high concentration of energy, not due to low-level errors delay time.
The following is the SAM version of the code:
#include <cstdio>#include<cstring>#include<algorithm>#defineRep (i,l,r) for (int i=l; i<=r; i++)typedefLong Longll;using namespacestd;Const intn=600100;intCnt=1, lst=1, n,tot[n],mx[n],p[n],pos[n],son[n][ -],fa[n],f[n][ -],q[n],r[n];ll ans; CharS[n],s[n];voidExtintCintx) { intp=lst,np=lst=++cnt; mx[np]=mx[p]+1; r[np]=1; pos[x]=NP; while(!son[p][c] && p) son[p][c]=np,p=Fa[p]; if(!p) fa[np]=1; Else{ intq=Son[p][c]; if(mx[q]==mx[p]+1) fa[np]=Q; Else{ intnq=++cnt; mx[nq]=mx[p]+1; memcpy (Son[nq],son[q],sizeof(Son[q])); FA[NQ]=FA[Q]; fa[q]=fa[np]=NQ; while(son[p][c]==q && p) son[p][c]=nq,p=Fa[p]; } }}voidPre () {Rep (I,1, CNT) tot[mx[i]]++; Rep (I,1, N) tot[i]+=tot[i-1]; for(inti=cnt; I i--) q[tot[mx[i]]--]=i; for(inti=cnt; I i--) r[fa[q[i]]]+=R[q[i]]; Rep (I,1, CNT) {f[i][0]=Fa[i]; Rep (J,1, +) f[i][j]=f[f[i][j-1]][j-1]; }}void Get(intLintR) {L= (l>>1) + (l&1); r>>=1;intx=Pos[r]; for(intI= +; ~i; i--) if(mx[f[x][i]]>=r-l+1) x=F[x][i]; Ans=max (ans,1ll*r[x]* (r-l+1));}voidManacher () {intmxlen=0, id; Rep (I,1, N) { if(mxlen>i) P[i]=min (mxlen-i,p[2*id-i]); Else{p[i]=1;if(s[i]!='#')Get(i,i);} while(S[i+p[i]]==s[i-p[i]])Get(I-p[i],i+p[i]), p[i]++; if(P[i]+i>mxlen) mxlen=p[i]+i,id=i; }}intMain () {Freopen ("palindromes.in","R", stdin); Freopen ("Palindromes.out","W", stdout); scanf ("%s", s+1); N=strlen (s+1); Rep (I,1, n) Ext (s[i]-'a', i); Pre (); s[0]='$'; s[1]='#'; Rep (I,1, N) s[(i<<1)+1]='#',s[i<<1]=S[i]; N= (n<<1)+1; Manacher (); printf"%lld\n", ans); return 0;}
[bzoj3676][apio2014] palindrome string (manacher+sam)