HDU 2222 Keywords Search

Problem Description

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.

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

5

she

he

say

shr

her

yasherhs

Sample Output

3

Solution

AC自动机入门题。根据模式串建立AC自动机,把匹配串扔上去跑就可以了。统计答案的时候,要从该点到root的答案都要统计。而且每个节点答案只统计一次。

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
92
93
94
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long LL;
const int INF = 1009999999;
char str[1000005];
struct queue_type {
	int data[1000005], head, tail;
	inline void clear() { head = tail = 0; }
	inline bool empty() { return head == tail; }
	inline void push(int x) {
		data[tail++] = x;
		if (tail == 1000000)
			tail = 0;
	}
	inline int pop() {
		int ret = data[head++];
		if (head == 1000000)
			head = 0;
		return ret;
	}
} Q;
struct Trie_type {
	int tot, root;
	int go[500005][26], fail[500005], term[500005], vis[500005];
	void init() {
		for (int i = 0; i <= tot; ++i) {
			for (int j = 0; j < 26; ++j)
				go[i][j] = 0;
			vis[i] = fail[i] = term[i] = 0;
		}
		root = tot = 0;
		vis[0] = 1;
	}
	void insert(char *str) {
		char x; int now = root;
		for (int i = 0; str[i]; ++i) {
			x = str[i] - 'a';
			if (go[now][x] == 0)
				go[now][x] = ++tot;
			now = go[now][x];
		}
		++term[now];
	}
	void build_fail() {
		Q.clear();
		for (int i = 0; i < 26; ++i)
			if (go[root][i]) {
				Q.push(go[root][i]);
				fail[go[root][i]] = root;
			}
		int u, v;
		while (!Q.empty()) {
			u = Q.pop(); vis[u] = vis[fail[u]] && (term[u] == 0);
			for (int i = 0; i < 26; ++i) {
				v = go[u][i];
				if (v) {
					Q.push(v);
					fail[v] = go[fail[u]][i];
				} else go[u][i] = go[fail[u]][i];
			}
		}
	}
	int run(char *str) {
		int now = root, ret = 0;
		for (int i = 0; str[i]; ++i) {
			int x = str[i] - 'a';
			now = go[now][x];
			for (int j = now; !vis[j]; j = fail[j]) {
				ret += term[j];
				term[j] = 0;
				vis[j] = 1;
			}
		}
		return ret;
	}
} Trie;
int N, T;
int main() {
	scanf("%d", &T);
	while (T--) {
		Trie.init();
		scanf("%d", &N);
		for (int i = 0; i < N; ++i) {
			scanf("%s", str);
			Trie.insert(str);
		}
		Trie.build_fail();
		scanf("%s", str);
		
		printf("%d\n", Trie.run(str));
	}
	return 0;
}