#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int inf = 1e9;
void transform(string &str) {
for (char &c: str) {
if (c == 'A') c = 'a';
else if (c == 'G') c = 'b';
else if (c == 'C') c = 'c';
else c = 'd';
}
}
struct node {
int sz = 0, mn = inf, mx = -inf, go[4] = {-1, -1, -1, -1};
node() = default;
};
vector<node> t = {{}};
void add(string &str, int key) {
int v = 0;
for (int i = 0; i < (int) str.size(); ++i) {
int c = str[i] - 'a';
if (t[v].go[c] == -1) {
t.emplace_back();
t[v].go[c] = (int) t.size() - 1;
}
t[v].sz += 1;
t[v].mn = min(t[v].mn, key);
t[v].mx = max(t[v].mx, key);
v = t[v].go[c];
}
t[v].sz += 1;
t[v].mn = min(t[v].mn, key);
t[v].mx = max(t[v].mx, key);
}
int find(string &str) {
int v = 0;
for (int i = 0; i < (int) str.size() && v != -1; ++i) {
v = t[v].go[str[i] - 'a'];
}
return v;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
vector<string> s(n);
for (int i = 0; i < n; ++i) {
cin >> s[i];
transform(s[i]);
}
sort(s.begin(), s.end());
vector<string> a(m), b(m);
vector<int> ans(m);
vector<vector<pair<int, int>>> qu(n);
for (int i = 0; i < m; ++i) {
cin >> a[i] >> b[i];
transform(a[i]), transform(b[i]);
reverse(b[i].begin(), b[i].end());
}
for (int i = 0; i < n; ++i) {
add(s[i], i);
}
for (int i = 0; i < m; ++i) {
int v = find(a[i]);
if (v == -1) {
continue;
}
assert(t[v].sz == t[v].mx - t[v].mn + 1);
if (t[v].mn > 0) {
qu[t[v].mn - 1].emplace_back(i, -1);
}
qu[t[v].mx].emplace_back(i, 1);
}
t = {{}};
for (int i = 0; i < n; ++i) {
reverse(s[i].begin(), s[i].end());
add(s[i], i);
for (auto [j, k] : qu[i]) {
int v = find(b[j]);
if (v != -1) {
ans[j] += k * t[v].sz;
}
}
}
for (int i = 0; i < m; ++i) {
cout << ans[i] << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
316 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
101 ms |
66224 KB |
Output is correct |
2 |
Correct |
99 ms |
66612 KB |
Output is correct |
3 |
Correct |
95 ms |
66436 KB |
Output is correct |
4 |
Correct |
96 ms |
66568 KB |
Output is correct |
5 |
Correct |
83 ms |
63656 KB |
Output is correct |
6 |
Correct |
82 ms |
63632 KB |
Output is correct |
7 |
Correct |
54 ms |
11980 KB |
Output is correct |
8 |
Correct |
86 ms |
41884 KB |
Output is correct |
9 |
Correct |
92 ms |
41828 KB |
Output is correct |
10 |
Correct |
61 ms |
36816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
7880 KB |
Output is correct |
2 |
Correct |
20 ms |
5484 KB |
Output is correct |
3 |
Correct |
24 ms |
6596 KB |
Output is correct |
4 |
Correct |
18 ms |
5600 KB |
Output is correct |
5 |
Correct |
19 ms |
5200 KB |
Output is correct |
6 |
Correct |
25 ms |
6552 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
316 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
101 ms |
66224 KB |
Output is correct |
9 |
Correct |
99 ms |
66612 KB |
Output is correct |
10 |
Correct |
95 ms |
66436 KB |
Output is correct |
11 |
Correct |
96 ms |
66568 KB |
Output is correct |
12 |
Correct |
83 ms |
63656 KB |
Output is correct |
13 |
Correct |
82 ms |
63632 KB |
Output is correct |
14 |
Correct |
54 ms |
11980 KB |
Output is correct |
15 |
Correct |
86 ms |
41884 KB |
Output is correct |
16 |
Correct |
92 ms |
41828 KB |
Output is correct |
17 |
Correct |
61 ms |
36816 KB |
Output is correct |
18 |
Correct |
31 ms |
7880 KB |
Output is correct |
19 |
Correct |
20 ms |
5484 KB |
Output is correct |
20 |
Correct |
24 ms |
6596 KB |
Output is correct |
21 |
Correct |
18 ms |
5600 KB |
Output is correct |
22 |
Correct |
19 ms |
5200 KB |
Output is correct |
23 |
Correct |
25 ms |
6552 KB |
Output is correct |
24 |
Correct |
105 ms |
68620 KB |
Output is correct |
25 |
Correct |
109 ms |
71780 KB |
Output is correct |
26 |
Correct |
94 ms |
67584 KB |
Output is correct |
27 |
Correct |
107 ms |
68092 KB |
Output is correct |
28 |
Correct |
141 ms |
31972 KB |
Output is correct |
29 |
Correct |
84 ms |
18144 KB |
Output is correct |