-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAho Corasick.cpp
More file actions
71 lines (71 loc) · 1.63 KB
/
Aho Corasick.cpp
File metadata and controls
71 lines (71 loc) · 1.63 KB
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
#include <bits/stdc++.h>
using namespace std;
struct node {
node* child[26];
node* fail;
vector<int> pat;
};
node* head;
queue<node*> Q;
void insert_(char* str, int idx) {
node* it = head;
int sz = strlen(str), letter;
for (int j = 0; j < sz; j++) {
letter = int(str[j]) - 'A';
if (!it->child[letter]) it->child[letter] = new node();
it = it->child[letter];
}
it->pat.push_back(idx);
}
void build() {
head->fail = head;
node *it, *fail;
for (int j = 0; j < 30; j++) {
if (head->child[j]) {
head->child[j]->fail = head;
Q.push(head->child[j]);
}
}
while (!Q.empty()) {
it = Q.front();
Q.pop();
for (int j = 0; j < 30; j++) {
if (!it->child[j]) continue;
fail = it->fail;
it->child[j]->fail = head;
// if(fail==NULL) cout<<"ijda3ni ya wad\n";
while (1) {
if (fail->child[j]) {
it->child[j]->fail = fail->child[j];
break;
}
if (fail == head) break;
fail = fail->fail;
}
Q.push(it->child[j]);
it->child[j]->pat.insert(it->child[j]->pat.end(),
it->child[j]->fail->pat.begin(),
it->child[j]->fail->pat.end());
}
}
}
void Aho_Corasick(char* str) {
int sz = strlen(str), letter, szo;
node* it = head;
for (int j = 0; j < sz; j++) {
letter = str[j] - 'A';
if (it->child[letter])
it = it->child[letter];
else {
it = it->fail;
while (1) {
if (it->child[letter]) {
it = it->child[letter];
break;
}
if (it == head) break;
it = it->fail;
}
}
}
}