-
Notifications
You must be signed in to change notification settings - Fork 0
/
ACAM.cpp
98 lines (91 loc) · 1.84 KB
/
ACAM.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
#include <bits/stdc++.h>
using namespace std;
const int N = 1000010, M = 26;
struct ACAM {
int nxt[N][M], fail[N], qrr[N], id[N];
int root, idx, t;
int cnt[N];
ACAM() {
clear();
}
void clear() {
memset(nxt[0], 0, sizeof nxt[0]);
root = idx = 0;
}
int newnode() {
idx ++;
memset(nxt[idx], 0, sizeof nxt[idx]);
return idx;
}
// string version: insert s[x] to trie-tree
int insert(string s, int x) {
int now = root;
for (int i = 0; i < s.size(); i++) {
int u = s[i] - 'a';
if (!nxt[now][u]) nxt[now][u] = newnode();
now = nxt[now][u];
}
return id[x] = now;
}
// char version
int insert(char *s, int x) {
int now = root;
for (int i = 0; s[i]; i++) {
int u = s[i] - 'a';
if(!nxt[now][u]) nxt[now][u] = newnode();
now = nxt[now][u];
}
return id[x] = now;
}
// build the acam
void build() {
fail[root] = root;
queue<int> q;
for (int i = 0; i < M; i++) if (nxt[root][i]) q.push(nxt[root][i]);
while(q.size()) {
int u = q.front(); q.pop();
qrr[t ++] = u;
for (int i = 0; i < M; i++) {
if (!nxt[u][i]) {
nxt[u][i] = nxt[fail[u]][i];
}
else {
int tmp = nxt[u][i];
fail[tmp] = nxt[fail[u]][i];
q.push(tmp);
}
}
}
}
// string version
void solve(string s, int m) {
int now = root;
for (int i = 0; i < s.size(); i++) {
now = nxt[now][s[i] - 'a'];
cnt[now]++;
}
for (int i = t - 1; i; i--) {
cnt[fail[qrr[i]]] += cnt[qrr[i]];
}
for (int i = 0; i < m; i++) {
cout << cnt[id[i]] << '\n';
}
}
} sol;
int n;
string s;
void solve () {
cin >> n;
for (int i = 0; i < n; i ++) {
string x; cin >> x;
sol.insert(x, i);
}
sol.build();
cin >> s;
sol.solve(s, n);
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
solve();
return 0;
}