显然AC自动机,但什么叫无限生成呢?
显然就是在AC自动机上匹配,出现了一个环(不能走结尾节点)
直接搜索即可

 1 var trie:array[0..30010,'0'..'1'] of longint;
 2     q,f:array[0..30010] of longint;
 3     can,v,r:array[0..30010] of boolean;
 4     l,n,i,t,j,k:longint;
 5     s:ansistring;
 6 
 7 procedure ac;
 8   var j,x,y,h,r:longint;
 9       c:char;
10   begin
11     h:=1;
12     r:=0;
13     for c:='0' to '1' do
14       if trie[0,c]<>0 then
15       begin
16         inc(r);
17         q[r]:=trie[0,c];
18       end;
19     while h<=r do
20     begin
21       x:=q[h];
22       for c:='0' to '1' do
23         if trie[x,c]<>0 then
24         begin
25           y:=trie[x,c];
26           inc(r);
27           q[r]:=y;
28           j:=f[x];
29           while (j>0) and (trie[j,c]=0) do j:=f[j];
30           f[y]:=trie[j,c];
31           if can[trie[j,c]] then can[y]:=true;
32         end;
33       inc(h);
34     end;
35   end;
36 
37 function dfs(x:longint):boolean;
38   var c:char;
39       j:longint;
40   begin
41     if can[x] then exit(false);
42     if v[x] then exit(true)
43     else v[x]:=true;
44     for c:='0' to '1' do
45     begin
46       j:=x;
47       while (j>0) and (trie[j,c]=0) do j:=f[j];
48       if r[trie[j,c]] then continue;
49       if dfs(trie[j,c]) then exit(true);
50     end;
51     v[x]:=false;
52     r[x]:=true;
53     exit(false);
54   end;
55 
56 begin
57   readln(n);
58   t:=0;
59   for i:=1 to n do
60   begin
61     readln(s);
62     l:=length(s);
63     j:=0;
64     for k:=1 to l do
65     begin
66       if trie[j,s[k]]=0 then
67       begin
68         inc(t);
69         trie[j,s[k]]:=t;
70       end;
71       j:=trie[j,s[k]];
72     end;
73     can[j]:=true;
74   end;
75   ac;
76   if dfs(0) then writeln('TAK') else writeln('NIE');
77 end.
View Code

 

posted on 2015-04-04 22:53  acphile  阅读(151)  评论(0编辑  收藏  举报