-
Notifications
You must be signed in to change notification settings - Fork 0
/
aho_corasick.cpp
85 lines (85 loc) · 1.7 KB
/
aho_corasick.cpp
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
// BOJ 9250 AC Code
// https://www.acmicpc.net/problem/9250
#include <bits/stdc++.h>
using namespace std;
const char st = 'a';
const int MAXC = 'z' - 'a' + 1;
struct trie {
trie* child[MAXC];
trie* fail;
bool term;
trie() {
fill(child, child + MAXC, nullptr);
fail = nullptr;
term = false;
}
~trie() {
for (int i = 0; i < MAXC; i++)
if (child[i]) delete child[i];
}
void insert(const string& s, int key = 0) {
if (s.size() == key) term = true;
else {
int next = s[key] - st;
if (!child[next]) child[next] = new trie;
child[next]->insert(s, key + 1);
}
}
};
trie* root = new trie;
void getFail() {
queue<trie*> q;
q.push(root);
root->fail = root;
while (!q.empty()) {
trie* now = q.front();
q.pop();
for (int i = 0; i < MAXC; i++) {
trie* next = now->child[i];
if (!next) continue;
if (now == root) next->fail = root;
else {
trie* t = now->fail;
while (t != root && !t->child[i])
t = t->fail;
if (t->child[i]) t = t->child[i];
next->fail = t;
}
if (next->fail->term) next->term = true;
q.push(next);
}
}
}
bool isMatch(const string& s) {
trie* now = root;
bool ret = false;
for (int c = 0; c < s.size(); c++) {
int next = s[c] - st;
while (now != root && !now->child[next])
now = now->fail;
if (now->child[next])
now = now->child[next];
if (now->term) {
ret = true;
break;
}
}
return ret;
}
int main() {
cin.tie(NULL); cout.tie(NULL);
ios_base::sync_with_stdio(false);
int N; cin >> N;
for (int i = 0; i < N; i++) {
string s; cin >> s;
root->insert(s);
}
getFail();
int M; cin >> M;
for (int i = 0; i < M; i++) {
string s; cin >> s;
if (isMatch(s)) cout << "YES\n";
else cout << "NO\n";
}
delete root;
}