-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAho-Corasick Automaton.h
78 lines (72 loc) · 2 KB
/
Aho-Corasick Automaton.h
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
class AhoCorasickAutomaton
{
static const int charset = 26; // ['a', 'z']
struct Node
{
int count;
Node *fail;
Node *next[charset];
Node() : count(), fail(), next()
{}
};
public:
AhoCorasickAutomaton(const vector<string> &v) : root(new Node)
{ for (unsigned i = 0; i < v.size(); i++) insert(v[i]); build(); }
int match(const string &s)
{
int cnt = 0;
Node *p = root;
for (unsigned i = 0; i < s.length(); i++) {
int index = s[i] - 'a';
while (!p->next[index] && p != root) p = p->fail;
p = p->next[index];
p = p ? p : root;
for (Node *t = p; t != root && t->count != -1; t = t->fail) {
// match
cnt += t->count;
t->count = -1;
}
}
return cnt;
}
private:
void insert(const string &s)
{
Node *p = root;
for (unsigned i = 0; i < s.length(); i++) {
int index = s[i] - 'a';
if (!p->next[index]) p->next[index] = new Node;
p = p->next[index];
}
p->count++;
}
void build()
{
root->fail = 0;
queue<Node *> q;
q.push(root);
while (!q.empty()) {
Node *t = q.front(); q.pop();
Node *p = 0;
for (int i = 0; i < charset; i++) {
if (t->next[i]) {
if (t == root) t->next[i]->fail = root;
else {
p = t->fail;
while (p) {
if (p->next[i]) {
t->next[i]->fail = p->next[i];
break;
}
p = p->fail;
}
if (!p) t->next[i]->fail = root;
}
q.push(t->next[i]);
}
}
}
}
protected:
Node *root;
};