Question: http://uoj.ac/problem/131 (bzoj swollen qaq)
If even the suffix automata why can be turned into a suffix tree do not know, first to cut 3238 difference: http://blog.csdn.net/pbihao/article/details/53905995
We construct the suffix automata first, then turn to the suffix tree, run the DP on the suffix tree, just maintain a maximum minimum and size.
#include <cstdio> #include <cstring> #include <iostream> #define MAXN 600020 #define LL long-long using NA
Mespace std;
int N,SON[MAXN][26],FAIL[MAXN],SIZE[MAXN],LEN[MAXN],TOT=1,RT=1,LAST=1,CNT=1,HEAD[MAXN],VAL[MAXN];
LL ANS1[MAXN],ANS2[MAXN],MAX1[MAXN],MIN1[MAXN];
BOOL IS[MAXN];
int DEP[MAXN],W[MAXN];
Char S[MAXN]; struct Edge{int v,next;}
E[MAXN*2];
void Adde (int a,int b) {e[cnt].v=b,e[cnt].next=head[a];head[a]=cnt++;}
void Insert (int x,int v) {int p=last,np=++tot,q,nq;
Size[np]=1,len[np]=len[p]+1,is[np]=true;
while (P&&!son[p][x]) son[p][x]=np,p=fail[p];
if (p==0) Fail[np]=rt;
else{int q=son[p][x];
if (len[q]==len[p]+1) fail[np]=q;
else{nq=++tot;len[nq]=len[p]+1;
memcpy (son[nq],son[q],sizeof (son[q));
FAIL[NQ]=FAIL[Q];
FAIL[Q]=FAIL[NP]=NQ;
while (P&&SON[P][X]==Q) son[p][x]=nq,p=fail[p];
}} last=np;w[np]=v;
} void Dfs (int u,int fa) {dep[u]=len[u];
if (Is[u]) max1[u]=min1[u]=w[u];
else Max1[u]=-1e15,min1[u]=1e15; for (int V,i=heaD[u];i;i=e[i].next) {DFS (v=e[i].v,u); if (MAX1[U]!=-1E15&&MAX1[V]!=-1E15&&MIN1[V]!=1E15&&MIN1[U]!=1E15) Ans1[dep[u]]=max (Max (
Ans1[dep[u]], (LL) max1[u]*max1[v]), (LL) min1[u]*min1[v]);
ans2[dep[u]]+= (LL) size[u]*size[v];
Max1[u]=max (Max1[u],max1[v]);
Min1[u]=min (Min1[u],min1[v]);
SIZE[U]+=SIZE[V];
int main () {scanf ("%d", &n);
scanf ("%s", s+1);
for (int i=1;i<=n;i++) scanf ("%d", val+i);
for (int i=n;i>=1;i--) Insert (s[i]-' A ', val[i]);
for (int i=2;i<=tot;i++) Adde (fail[i],i);
for (int i=0;i<=n;i++) Ans1[i]=-1e18;
Dep[0]=-1;
DFS (1,0);
for (int i=n-1;i>=0;i--) Ans1[i]=max (ans1[i],ans1[i+1]), ans2[i]+=ans2[i+1];
for (int i=0;i<n;i++) if (Ans2[i]) printf ("%lld%lld\n", ans2[i],ans1[i));
Else puts ("0 0");
return 0; }