#include <bits/stdc++.h>
using namespace std;
const int N = 2000001, A = 4;
const string R = "AGCU";
int suffix[N][A], prefix[N][A], root[N], sub[N], ans[N];
vector<pair<int, vector<int>>> queries[N];
vector<int> read() {
string s;
cin >> s;
vector<int> ans;
for (auto c : s) {
ans.push_back(find(R.begin(), R.end(), c) - R.begin());
}
return ans;
}
int merge(int u, int v) {
if (u == 0 || v == 0) {
return u > 0 ? u : v;
} else {
sub[u] += sub[v];
for (int i = 0; i < A; ++i) {
suffix[u][i] = merge(suffix[u][i], suffix[v][i]);
}
return u;
}
}
void dfs(int u) {
for (auto c : prefix[u]) {
if (c > 0) {
dfs(c);
root[u] = merge(root[u], root[c]);
}
}
for (auto &[i, q] : queries[u]) {
int v = root[u];
for (auto c : q) {
v = suffix[v][c];
}
ans[i] = sub[v];
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
int x = 1, y = 1;
for (int i = 0; i < n; ++i) {
vector<int> s = read();
int u = 0;
for (auto c : s) {
if (prefix[u][c] == 0) {
prefix[u][c] = x++;
}
u = prefix[u][c];
}
if (root[u] == 0) {
root[u] = y++;
}
reverse(s.begin(), s.end());
u = root[u];
++sub[u];
for (auto c : s) {
if (suffix[u][c] == 0) {
suffix[u][c] = y++;
}
u = suffix[u][c];
++sub[u];
}
}
for (int i = 0; i < m; ++i) {
vector<int> p = read(), q = read();
int u = 0;
for (auto c : p) {
u = prefix[u][c];
}
if (u > 0) {
reverse(q.begin(), q.end());
queries[u].push_back({i, q});
}
}
dfs(0);
for (int i = 0; i < m; ++i) {
cout << ans[i] << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
47308 KB |
Output is correct |
2 |
Correct |
27 ms |
47292 KB |
Output is correct |
3 |
Correct |
27 ms |
47204 KB |
Output is correct |
4 |
Correct |
27 ms |
47284 KB |
Output is correct |
5 |
Correct |
26 ms |
47208 KB |
Output is correct |
6 |
Correct |
27 ms |
47284 KB |
Output is correct |
7 |
Correct |
26 ms |
47240 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
107 ms |
90492 KB |
Output is correct |
2 |
Correct |
105 ms |
88744 KB |
Output is correct |
3 |
Correct |
216 ms |
135552 KB |
Output is correct |
4 |
Correct |
213 ms |
132048 KB |
Output is correct |
5 |
Correct |
162 ms |
100872 KB |
Output is correct |
6 |
Correct |
156 ms |
101444 KB |
Output is correct |
7 |
Correct |
139 ms |
90440 KB |
Output is correct |
8 |
Correct |
166 ms |
113296 KB |
Output is correct |
9 |
Correct |
157 ms |
106612 KB |
Output is correct |
10 |
Correct |
113 ms |
88380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
64 ms |
51364 KB |
Output is correct |
2 |
Correct |
63 ms |
50000 KB |
Output is correct |
3 |
Correct |
61 ms |
50836 KB |
Output is correct |
4 |
Correct |
56 ms |
50072 KB |
Output is correct |
5 |
Correct |
54 ms |
50312 KB |
Output is correct |
6 |
Correct |
59 ms |
50996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
47308 KB |
Output is correct |
2 |
Correct |
27 ms |
47292 KB |
Output is correct |
3 |
Correct |
27 ms |
47204 KB |
Output is correct |
4 |
Correct |
27 ms |
47284 KB |
Output is correct |
5 |
Correct |
26 ms |
47208 KB |
Output is correct |
6 |
Correct |
27 ms |
47284 KB |
Output is correct |
7 |
Correct |
26 ms |
47240 KB |
Output is correct |
8 |
Correct |
107 ms |
90492 KB |
Output is correct |
9 |
Correct |
105 ms |
88744 KB |
Output is correct |
10 |
Correct |
216 ms |
135552 KB |
Output is correct |
11 |
Correct |
213 ms |
132048 KB |
Output is correct |
12 |
Correct |
162 ms |
100872 KB |
Output is correct |
13 |
Correct |
156 ms |
101444 KB |
Output is correct |
14 |
Correct |
139 ms |
90440 KB |
Output is correct |
15 |
Correct |
166 ms |
113296 KB |
Output is correct |
16 |
Correct |
157 ms |
106612 KB |
Output is correct |
17 |
Correct |
113 ms |
88380 KB |
Output is correct |
18 |
Correct |
64 ms |
51364 KB |
Output is correct |
19 |
Correct |
63 ms |
50000 KB |
Output is correct |
20 |
Correct |
61 ms |
50836 KB |
Output is correct |
21 |
Correct |
56 ms |
50072 KB |
Output is correct |
22 |
Correct |
54 ms |
50312 KB |
Output is correct |
23 |
Correct |
59 ms |
50996 KB |
Output is correct |
24 |
Correct |
115 ms |
85244 KB |
Output is correct |
25 |
Correct |
138 ms |
87716 KB |
Output is correct |
26 |
Correct |
129 ms |
84020 KB |
Output is correct |
27 |
Correct |
205 ms |
122836 KB |
Output is correct |
28 |
Correct |
184 ms |
64184 KB |
Output is correct |
29 |
Correct |
187 ms |
59716 KB |
Output is correct |