以前看到这道题想到的是SA,做起来不是很美观
学了SAM之后,这题简直是随便搞
var go:array[..,''..''] of longint;
s,sa,mx,w,fa:array[..] of longint;
i,n,last,t:longint;
ch:char; procedure add(c:char);
var p,np,nq,q:longint;
begin
p:=last;
inc(t); np:=t; last:=t;
mx[np]:=mx[p]+;
w[np]:=;
while (p>) and (go[p,c]=) do
begin
go[p,c]:=np;
p:=fa[p];
end;
if p= then fa[np]:=
else begin
q:=go[p,c];
if mx[q]=mx[p]+ then fa[np]:=q
else begin
inc(t); nq:=t;
mx[nq]:=mx[p]+;
go[nq]:=go[q];
fa[nq]:=fa[q];
fa[q]:=nq; fa[np]:=nq;
while go[p,c]=q do
begin
go[p,c]:=nq;
p:=fa[p];
end;
end;
end;
end; procedure pre;
var i,x:longint;
begin
for i:= to t do
inc(s[mx[i]]);
for i:= to n do
inc(s[i],s[i-]);
for i:=t downto do
begin
sa[s[mx[i]]]:=i;
dec(s[mx[i]]);
end;
for i:=t downto do
begin
x:=sa[i];
inc(w[fa[x]],w[x]);
end;
end; procedure dfs(x:longint);
var y:longint;
c:char;
begin
for c:='' to '' do
if go[x,c]> then
begin
if w[go[x,c]]> then writeln(w[go[x,c]]);
dfs(go[x,c]);
end;
end; begin
readln(n);
t:=; last:=;
for i:= to n do
begin
read(ch);
add(ch);
end;
pre;
dfs();
end.