-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaho_corasick.cpp
169 lines (138 loc) · 3.66 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include<bits/stdc++.h>
using namespace std;
class Aho
{
private:
struct AhoNode
{
map<int, int> children;
char ch;
int suffixLink;
int wordEnd;
bool leaf;
int parent;
int wordId;
AhoNode(){
ch='.';
suffixLink=-1;
wordEnd=-1;
leaf=false;
parent=-1;
wordId=-1;
}
};
vector<AhoNode> trie;
vector<int> wordLen;
int root;
int sz;
void buildLink(int node){
if(node==root){
trie[node].suffixLink=root;
trie[node].wordEnd=root;
return;
}
if(trie[node].parent==root){
trie[node].suffixLink=root;
if(trie[node].leaf){
trie[node].wordEnd=node;
}
else{
trie[node].wordEnd=trie[trie[node].suffixLink].wordEnd;
}
return;
}
int next=trie[trie[node].parent].suffixLink;
int ch=trie[node].ch;
while(true){
if(trie[next].children.count(ch)){
trie[node].suffixLink=trie[next].children[ch];
break;
}
if(next==root){
trie[node].suffixLink=root;
break;
}
next=trie[next].suffixLink;
}
if(trie[node].leaf){
trie[node].wordEnd=node;
}
else{
trie[node].wordEnd=trie[trie[node].suffixLink].wordEnd;
}
}
public:
Aho(){
root=0;
trie.push_back(AhoNode());
sz=1;
}
void add(string s, int id){
int cur=root;
for(int i=0;i<s.length();++i){
int ch=s[i];
if(trie[cur].children.count(ch)==0){
trie.push_back(AhoNode());
trie[sz].ch=s[i];
trie[sz].parent=cur;
trie[cur].children[ch]=sz;
sz++;
}
cur=trie[cur].children[ch];
}
trie[cur].leaf=true;
trie[cur].wordId=id;
wordLen.push_back(s.length());
}
void buildAho(){
queue<int> q;
q.push(root);
while(!q.empty()){
int node=q.front(); q.pop();
buildLink(node);
for(map<int, int>::iterator it=trie[node].children.begin(); it!=trie[node].children.end();++it){
q.push(it->second);
}
}
}
void findMatches(string text){
int cur=root;
for(int i=0;i<text.length();++i){
int ch=text[i];
while(true){
if(trie[cur].children.count(ch)){
cur=trie[cur].children[ch];
break;
}
if(cur==root){
break;
}
cur=trie[cur].suffixLink;
}
int found=cur;
while(true){
int wordEnd=trie[found].wordEnd;
if(wordEnd==root){
break;
}
int len=wordLen[trie[wordEnd].wordId];
int idx=i-len+1;
cout<<text.substr(idx, len)<<' ';
found=trie[found].suffixLink;
}
}
}
};
int main(){
string s[]={"cat", "mat", "example", "random", "cat"};
int n=sizeof(s)/sizeof(s[0]);
Aho aho;
for(int i=0;i<n;++i){
aho.add(s[i], i);
// aho.addString(s[i]);
}
aho.buildAho();
string text="dsadscardsadamatdsecatdfhwgaedhfrandomdaexamplefdsjkfh";
aho.findMatches(text);
//output- mat cat random example
}