AC自动机模板题。
被卡内存了 死活A不掉。。
AC自动机参考教程:
http://www.cppblog.com/menjitianya/archive/2014/07/10/207604.html
1 const maxn=10008; 2 type arr=record 3 next:array[0..26] of longint; 4 fail,cnt:longint; 5 end; 6 var cas:longint; 7 s:array[0..maxn] of string; 8 T:array[0..200000] of arr; 9 id:array['a'..'z'] of longint; 10 rt,tmp:longint; 11 b:array[0..100000] of longint; 12 procedure ins(str:string); 13 var now,i:longint; 14 begin 15 now:=rt; 16 for i:=1 to length(str) do 17 begin 18 if T[now].next[id[str[i]]]=0 then 19 begin 20 inc(tmp); 21 T[now].next[id[str[i]]]:=tmp; 22 end; 23 now:=T[now].next[id[str[i]]]; 24 end; 25 inc(T[now].cnt); 26 end; 27 procedure build; 28 var l,r,u,v,i:longint; 29 begin 30 fillchar(b,sizeof(b),0); 31 r:=0; 32 for i:=1 to 26 do 33 if T[rt].next[i]<>0 then 34 begin 35 T[T[rt].next[i]].fail:=rt; 36 inc(r); 37 b[r]:=T[rt].next[i]; 38 end; 39 l:=1; 40 while l<=r do 41 begin 42 u:=b[l]; 43 for i:=1 to 26 do 44 begin 45 v:=T[u].next[i]; 46 if v=0 then 47 T[u].next[i]:=T[T[u].fail].next[i] 48 else 49 begin 50 T[v].fail:=T[T[u].fail].next[i]; 51 inc(r); 52 b[r]:=v; 53 end; 54 end; 55 inc(l); 56 end; 57 end; 58 function query(st:ansistring):longint; 59 var i,ans,now,j:longint; 60 begin 61 ans:=0; now:=rt; 62 for i:=1 to length(st) do 63 begin 64 now:=T[now].next[id[st[i]]]; 65 j:=now; 66 while T[j].cnt>0 do 67 begin 68 ans:=ans+T[j].cnt; 69 T[j].cnt:=0; 70 j:=T[j].fail; 71 end; 72 ans:=ans+T[T[j].fail].cnt; 73 T[T[j].fail].cnt:=0; 74 end; 75 exit(ans); 76 end; 77 procedure main; 78 var i,n:longint; 79 st:ansistring; 80 begin 81 fillchar(T,sizeof(T),0); 82 for i:=1 to 26 do id[chr(96+i)]:=i; 83 readln(n); 84 for i:=1 to n do readln(s[i]); 85 rt:=0; tmp:=0; 86 for i:=1 to n do ins(s[i]); 87 build; 88 readln(st); 89 writeln(query(st)); 90 end; 91 begin 92 readln(cas); 93 while cas>0 do begin dec(cas); main; end; 94 end.