以前看到这道题想到的是SA,做起来不是很美观

学了SAM之后,这题简直是随便搞

 1 var go:array[0..6010,'0'..'1'] of longint;
 2     s,sa,mx,w,fa:array[0..6010] of longint;
 3     i,n,last,t:longint;
 4     ch:char;
 5 
 6 procedure add(c:char);
 7   var p,np,nq,q:longint;
 8   begin
 9     p:=last;
10     inc(t); np:=t; last:=t;
11     mx[np]:=mx[p]+1;
12     w[np]:=1;
13     while (p>0) and (go[p,c]=0) do
14     begin
15       go[p,c]:=np;
16       p:=fa[p];
17     end;
18     if p=0 then fa[np]:=1
19     else begin
20       q:=go[p,c];
21       if mx[q]=mx[p]+1 then fa[np]:=q
22       else begin
23         inc(t); nq:=t;
24         mx[nq]:=mx[p]+1;
25         go[nq]:=go[q];
26         fa[nq]:=fa[q];
27         fa[q]:=nq; fa[np]:=nq;
28         while go[p,c]=q do
29         begin
30           go[p,c]:=nq;
31           p:=fa[p];
32         end;
33       end;
34     end;
35   end;
36 
37 procedure pre;
38   var i,x:longint;
39   begin
40     for i:=1 to t do
41       inc(s[mx[i]]);
42     for i:=1 to n do
43       inc(s[i],s[i-1]);
44     for i:=t downto 1 do
45     begin
46       sa[s[mx[i]]]:=i;
47       dec(s[mx[i]]);
48     end;
49     for i:=t downto 1 do
50     begin
51       x:=sa[i];
52       inc(w[fa[x]],w[x]);
53     end;
54   end;
55 
56 procedure dfs(x:longint);
57   var y:longint;
58       c:char;
59   begin
60     for c:='0' to '1' do
61       if go[x,c]>0 then
62       begin
63         if w[go[x,c]]>1 then writeln(w[go[x,c]]);
64         dfs(go[x,c]);
65       end;
66   end;
67 
68 begin
69   readln(n);
70   t:=1; last:=1;
71   for i:=1 to n do
72   begin
73     read(ch);
74     add(ch);
75   end;
76   pre;
77   dfs(1);
78 end.
View Code

 

posted on 2015-06-30 16:50  acphile  阅读(181)  评论(0编辑  收藏  举报