-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathaho_corasick.cpp
More file actions
136 lines (133 loc) · 3.34 KB
/
aho_corasick.cpp
File metadata and controls
136 lines (133 loc) · 3.34 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
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
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define int long long int
#define pb push_back
#define pi pair<int, int>
#define pii pair<pi, int>
#define fir first
#define sec second
#define DEBUG 0
#define MAXN 5001
#define mod 1000000007
namespace aho
{
int go(int v, char ch);
const int K = 26; // tamanho do alfabeto
struct trie
{
char me; // char correspondente ao no atual
int go[K]; // proximo vertice que eu devo ir estando em um estado (v, c)
int down[K]; // proximo vertice da trie
int is_leaf = 0; // se o vertice atual da trie eh uma folha (fim de uma ou mais strings)
int parent = -1; // no ancestral do no atual
int link = -1; // link de sufixo do no atual (outro no com o maior matching de sufixo)
int exit_link = -1; // folha mais proxima que pode ser alcancada a partir de v usando links de sufixo
trie(int p = -1, char ch = '$') : parent(p), me(ch)
{
fill(begin(go), end(go), -1);
fill(begin(down), end(down), -1);
}
};
vector<trie> ac;
void init() // criar a raiz da trie
{
ac.resize(1);
}
void add_string(string s) // adicionar string na trie
{
int v = 0;
for (auto const &ch : s)
{
int c = ch - 'a';
if (ac[v].down[c] == -1)
{
ac[v].down[c] = ac.size();
ac.emplace_back(v, ch);
}
v = ac[v].down[c];
}
ac[v].is_leaf++;
}
int get_link(int v) // pegar o suffix link saindo de v
{
if (ac[v].link == -1)
ac[v].link = (!v || !ac[v].parent) ? 0 : go(get_link(ac[v].parent), ac[v].me);
return ac[v].link;
}
int go(int v, char ch) // proximo estado saindo do estado(v, ch)
{
int c = ch - 'a';
if (ac[v].go[c] == -1)
{
if (ac[v].down[c] != -1)
ac[v].go[c] = ac[v].down[c];
else
ac[v].go[c] = (!v) ? 0 : go(get_link(v), ch);
}
return ac[v].go[c];
}
int get_exit_link(int v) // suffix link mais proximo de v que seja uma folha
{
if (ac[v].exit_link == -1)
{
int curr = get_link(v);
if (!v || !curr)
ac[v].exit_link = 0;
else if (ac[curr].is_leaf)
ac[v].exit_link = curr;
else
ac[v].exit_link = get_exit_link(curr);
}
return ac[v].exit_link;
}
int query(string s) // query O(n + ans)
{
int ans = 0, curr = 0, at;
for (auto const &i : s)
{
curr = go(curr, i);
ans += ac[curr].is_leaf;
at = get_exit_link(curr);
while (at)
{
ans += ac[at].is_leaf;
at = get_exit_link(at);
}
}
return ans;
}
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, q;
cin >> n >> q;
aho::init();
for (int i = 0; i < n; i++)
{
string s;
cin >> s;
aho::add_string(s);
}
while (q--)
{
string t;
cin >> t;
cout << aho::query(t) << endl;
}
return 0;
}
// automato de aho-corasick
// imagine o seguinte problema:
// temos um conjunto de n strings
// e q queries para processar
// em cada uma das q queries, voce recebe uma string s
// e quer saber, o numero de ocorrencias de
// alguma string do conjunto como
// substring de s e em tempo linear