type node=record got:longint; next:array[‘a‘..‘z‘] of longint; end; var t:array[0..1000000] of node; s:string; n,m,i,tot:longint; procedure getintree(s:string); var i,now:longint; begin now:=1; for i:=1 to length(s) do if t[now].next[s[i]]<>0 then now:=t[now].next[s[i]] else begin inc(tot); t[tot].got:=0; fillchar(t[tot].next,sizeof(t[tot].next),0); t[now].next[s[i]]:=tot; now:=tot; end; inc(t[now].got); end; function check(s:string):longint; var i,now:longint; begin now:=1; for i:=1 to length(s) do if t[now].next[s[i]]<>0 then now:=t[now].next[s[i]] else exit(0); exit(t[now].got); end; begin tot:=1; t[1].got:=0; fillchar(t[1].next,sizeof(t[1].next),0); readln(n); for i:=1 to n do begin readln(s); getintree(s); end; readln(m); for i:=1 to m do begin readln(s); writeln(check(s)); end; end.
tyvj1161聚会的名单(trie树),布布扣,bubuko.com
原文:http://www.cnblogs.com/zyfzyf/p/3768522.html