Keywords Search

time limit per test 1 second memory limit per test 256 megabytes
In the modern time, Search engine came into the life of everybody like Google, Baidu, etc.
Wiskey also wants to bring this feature to his image retrieval system.
Every image have a long description, when users type some keywords to find the image, the system will match the keywords with description of image and show the image which the most keywords be matched.
To simplify the problem, giving you a description of image, and some keywords, you should tell me how many keywords will be match.

传送门:HDU2222

Input

First line will contain one integer means how many cases will follow by.
Each case will contain two integers N means the number of keywords and N keywords follow. (N <= 10000)
Each keyword will only contains characters ‘a’-‘z’, and the length will be not longer than 50.
The last line is the description, and the length will be not longer than 1000000.

Output

Print how many keywords are contained in the description.

Sample Input

1
2
3
4
5
6
7
8
1
5
she
he
say
shr
her
yasherhs

Sample Output

1
3

题解

记录一下AC自动机的板子 在Trie树上做KMP的多模式匹配 O(n)级别

AC code:(不包含输入类)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import java.io.*;  
import java.util.*;
public class Main {

public static void main(String[] args) {
FastScanner sc=new FastScanner();
PrintWriter pw=new PrintWriter(System.out);
AcTrie ac=new AcTrie();
int t=sc.nextInt();
for(int o=1;o<=t;o++){
ac.init();
int n=sc.nextInt();
for(int i=0;i<n;i++){
String s=sc.next();
ac.insert(s);
}
ac.build();
String q=sc.next();
pw.println(ac.query(q));
pw.flush();
}

}
}
class AcTrie{
int maxn=500100;
int maxm=26;
int[][] next=new int[maxn][maxm];
int[] fail=new int[maxn];
int[] end=new int[maxn];
int[] dep=new int[maxn];
int root,L;
void init() {
L = 0;
root = newnode(-1);
}
int newnode(int depth) {
for(int i = 0; i < maxm; i++)
next[L][i] = -1;
dep[L] = depth + 1;
end[L++] = 0;
return L-1;
}
void insert(String s) {
int now = root;
for(int i = 0; i <s.length(); i++) {
int k=s.charAt(i)-'a';
if(next[now][k] == -1)
next[now][k] = newnode(dep[now]);
now = next[now][k];
}
end[now]++;
}
void build() {
Queue<Integer>queue=new LinkedList<Integer>();
fail[root] = root;
for(int i = 0; i < maxm; i++) {
if(next[root][i] == -1)
next[root][i] = root;
else {
fail[next[root][i]] = root;
queue.add(next[root][i]);
}
}
while(!queue.isEmpty()) {
int now =queue.poll();
for(int i = 0; i < maxm; i++) {
if(next[now][i] == -1)
next[now][i] = next[fail[now]][i];
else {
fail[next[now][i]] = next[fail[now]][i];
queue.add(next[now][i]);
}
}
}
}
int query(String s){ //构建好trie树后 统计在s串中存在多少个trie树中的串
int res=0;
int now=root;
for(int i=0;i<s.length();i++){
now=next[now][s.charAt(i)-'a'];
int temp=now;
while(temp!=root){
res+=end[temp];
end[temp]=0;
temp=fail[temp];
}
}
return res;
}
}