后缀自动机+拓扑排序
#include <iostream>
#include <string.h>
#include <stdio.h>
#define maxn 250000*4
#define Smaxn 26
using namespace std;
struct node
{
node *par,*go[Smaxn];
int flag;
int num;
int val;
}*root,*tail,que[maxn],*top[maxn];
int tot;
char str[maxn];
void add(int c,int l)
{
node *p=tail,*np=&que[tot++];
np->val=l;
while(p&&p->go[c]==NULL)
p->go[c]=np,p=p->par;
if(p==NULL) np->par=root;
else
{
node *q=p->go[c];
if(p->val+1==q->val) np->par=q;
else
{
node *nq=&que[tot++];
*nq=*q;
nq->val=p->val+1;
np->par=q->par=nq;
while(p&&p->go[c]==q) p->go[c]=nq,p=p->par;
}
}
tail=np;
}
int c[maxn],len;
void init(){
len=1;
tot=0;
memset(que,0,sizeof(que));
root=tail=&que[tot++];
}
char st[maxn];
int ans[maxn];
void solve(int n){
int i,j;
memset(c,0,sizeof(c));
for(i=0;i<tot;i++)
c[que[i].val]++;
for(i=1;i<len;i++)
c[i]+=c[i-1];
for(i=0;i<tot;i++)
top[--c[que[i].val]]=&que[i];
for(node *p=root;;p=p->go[str[p->val]-'a']){
p->num=1;
if (p->val==len-1)break;
}
for(i=tot-1;i>=0;i--){
node *p=top[i];
if(p->par){
p->par->num+=p->num;
}
}
for(int i=1;i<tot;i++)
ans[que[i].val] = max(ans[que[i].val],que[i].num);
for(int i=1;i<=n;i++){
printf("%d\n",ans[i]);
ans[i]=0;
}
}
int main(){
while(~scanf("%s",str)) {
init();
int i,l=strlen(str);
for(i=0;i<l;i++)
add(str[i]-'a',len++);
solve(l);
}
return 0;
}