-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path2103.cpp
71 lines (67 loc) · 1.46 KB
/
2103.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
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int k, sz, last, len[N], link[N], cnt[N], nxt[N][26];
string s;
vector<pair<int, int>> order;
void sa_init() {
len[0] = 0;
link[0] = -1;
sz = 1; last = 0;
}
void sa_extend(int c) {
int cur = sz++, p;
len[cur] = len[last] + 1;
cnt[cur] = 1;
order.emplace_back(len[cur], cur);
for (p = last; p != -1 && !nxt[p][c]; p = link[p]) {
nxt[p][c] = cur;
}
if (p == -1) {
link[cur] = 0;
} else {
int q = nxt[p][c];
if (len[p] + 1 == len[q]) {
link[cur] = q;
} else {
int clone = sz++;
len[clone] = len[p] + 1;
link[clone] = link[q];
cnt[clone] = 0;
memcpy(nxt[clone], nxt[q], sizeof(nxt[q]));
order.emplace_back(len[clone], clone);
for (; p != -1 && nxt[p][c] == q; p = link[p]) {
nxt[p][c] = clone;
}
link[cur] = link[q] = clone;
}
}
last = cur;
}
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> s >> k;
sa_init();
for (char c: s) {
sa_extend(c - 'a');
}
sort(order.begin(), order.end(), greater<pair<int, int>>());
for (auto [len, p]: order) {
cnt[link[p]] += cnt[p];
}
while (k--) {
string p; cin >> p;
int cur = 0;
bool found = true;
for (char c: p) {
int cc = c - 'a';
if (!nxt[cur][cc]) {
found = false;
break;
}
cur = nxt[cur][cc];
}
cout << (found ? cnt[cur] : 0) << "\n";
}
return 0;
}