-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAho_Corasick.cpp
151 lines (135 loc) · 3.3 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
/*
Че пацаны физтех?
┓┏┓┏┓┃
┛┗┛┗┛┃
┓┏┓┏┓┃
┛┗┛┗┛┃
┓┏┓┏┓┃\○/
┛┗┛┗┛┃ / /
┓┏┓┏┓┃ノ
┛┗┛┗┛┃
┓┏┓┏┓┃
┛┗┛┗┛┃
┓┏┓┏┓┃
┛┗┛┗┛┃
┓┏┓┏┓┃
┛┗┛┗┛┃
┓┏┓┏┓┃┓
┛┗┛┗┛┃┃
*/
#include <iostream>
#include <string>
#include <map>
#include <unordered_map>
#include <vector>
#define N 1000000
using namespace std;
const int ALP = 26;
struct bohr_node {
bool leaf;
int pattern_num; //number of line that ended in this node
int next_ch[ALP]; //next characters
int suff_links[ALP];
int suff_link;
int g_suff_link; //good suffix link
int parent;
char par_symb; //symbol on edge from parent node to current
bohr_node() {
leaf = false;
pattern_num = suff_link = g_suff_link = parent = -1;
memset(next_ch, -1, sizeof(next_ch));
memset(suff_links, -1, sizeof(suff_links));
}
};
vector <bohr_node> bohr;
vector <string> patterns;
void add_string(string& s, int pat_num) {
int it = 0;
for (int i = 0; i < s.size(); i++) {
int c = (int)(s[i] - 'a');
if (bohr[it].next_ch[c] == -1) {
bohr_node p;
p.parent = it;
p.par_symb = s[i] - 'a';
bohr.push_back(p);
bohr[it].next_ch[c] = bohr.size() - 1;
}
it = bohr[it].next_ch[c];
}
bohr[it].leaf = true;
bohr[it].pattern_num = pat_num;
}
bool pattern_entry(string& s) {
int it = 0;
for (int i = 0; i < s.size(); i++) {
int c = (int)(s[i] - 'a');
if (bohr[it].next_ch[c] == -1)
return false;
it = bohr[it].next_ch[c];
}
return bohr[it].leaf;
}
int get_transition(int v, char c);
int get_suffix_link(int v) {
if (bohr[v].suff_link == -1) {
if (v == 0 || bohr[v].parent == 0)
bohr[v].suff_link = 0;
else
bohr[v].suff_link = get_transition(get_suffix_link(bohr[v].parent), bohr[v].par_symb);
}
return bohr[v].suff_link;
}
int get_transition(int v, char c) {
if (bohr[v].suff_links[c] == -1) {
if (bohr[v].next_ch[c] != -1)
bohr[v].suff_links[c] = bohr[v].next_ch[c];
else {
if (v == 0)
bohr[v].suff_links[c] = 0;
else
bohr[v].suff_links[c] = get_transition(get_suffix_link(v), c);
}
}
return bohr[v].suff_links[c];
}
int get_good_suff_link(int v) {
if (bohr[v].g_suff_link == -1) {
int next_suff = get_suffix_link(v);
if (next_suff == 0) bohr[v].g_suff_link = 0;
else {
if (bohr[next_suff].leaf == false)
bohr[v].g_suff_link = get_good_suff_link(next_suff);
else
bohr[v].g_suff_link = next_suff;
}
}
return bohr[v].g_suff_link;
}
int main() {
//initialization
bohr_node v;
v.parent = 0;
v.par_symb = '$';
bohr.push_back(v);
string text = "";
cin >> text;
int pattern_num;
cin >> pattern_num;
string pattern = "";
for(int i = 0; i < pattern_num; i++) {
cin >> pattern;
patterns.push_back(pattern);
add_string(pattern, i);
}
int res_pos = 0;
int u = 0;
for (int i = 0; i < text.size(); i++) {
res_pos = get_transition(res_pos, text[i] - 'a');
u = res_pos;
while (u != 0) {
if(bohr[u].leaf) cout << i - patterns[bohr[u].pattern_num].length() + 2 << " " << patterns[bohr[u].pattern_num] << endl;
u = get_good_suff_link(u);
}
}
return 0;
}