#include <bits/stdc++.h>
#define long long long
using namespace std;
const int N = (int) 1E5;
const int K = (int) 2E6;
int index(char c) {
if (c == 'A') {
return 0;
}
if (c == 'G') {
return 1;
}
if (c == 'C') {
return 2;
}
if (c == 'U') {
return 3;
}
return -1;
}
int x[2];
int e[2][K + 1][4];
int l[K + 1], r[K + 1];
vector<int> a[K + 1];
void insert(string s, int p, int t) {
int u = 0;
for (int i = 0; i < (int) s.size(); i++) {
if (t == 0) {
l[u] = l[u] >= 0 ? l[u] : p;
r[u] = max(r[u], p);
} else {
a[u].push_back(p);
}
int &v = e[t][u][index(s[i])];
if (v == 0) {
v = ++x[t];
}
u = v;
}
if (t == 0) {
l[u] = l[u] >= 0 ? l[u] : p;
r[u] = max(r[u], p);
} else {
a[u].push_back(p);
}
}
int find(string s, int t) {
int u = 0;
for (int i = 0; i < (int) s.size(); i++) {
int v = e[t][u][index(s[i])];
if (v == 0) {
return -1;
}
u = v;
}
return u;
}
int n, k;
string s[N];
int main() {
#ifdef LOCAL
freopen("A.in", "r", stdin);
#endif
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> s[i];
}
sort(s, s + n);
memset(l, -1, sizeof(l));
for (int i = 0; i < n; i++) {
insert(s[i], i, 0);
reverse(s[i].begin(), s[i].end());
insert(s[i], i, 1);
}
for (int i = 0; i < k; i++) {
string t0, t1;
cin >> t0 >> t1;
reverse(t1.begin(), t1.end());
int p0 = find(t0, 0), p1 = find(t1, 1);
if (p0 < 0 || p1 < 0) {
cout << 0 << "\n";
} else {
auto i0 = upper_bound(a[p1].begin(), a[p1].end(), r[p0]);
auto i1 = lower_bound(a[p1].begin(), a[p1].end(), l[p0]);
cout << (int) (i0 - i1) << "\n";
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
58204 KB |
Output is correct |
2 |
Correct |
13 ms |
58312 KB |
Output is correct |
3 |
Correct |
15 ms |
58204 KB |
Output is correct |
4 |
Correct |
14 ms |
58204 KB |
Output is correct |
5 |
Correct |
14 ms |
58204 KB |
Output is correct |
6 |
Correct |
14 ms |
58204 KB |
Output is correct |
7 |
Correct |
14 ms |
58340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
140 ms |
156668 KB |
Output is correct |
2 |
Correct |
141 ms |
151760 KB |
Output is correct |
3 |
Correct |
75 ms |
113756 KB |
Output is correct |
4 |
Correct |
65 ms |
111368 KB |
Output is correct |
5 |
Correct |
117 ms |
143188 KB |
Output is correct |
6 |
Correct |
117 ms |
144468 KB |
Output is correct |
7 |
Correct |
53 ms |
74088 KB |
Output is correct |
8 |
Correct |
114 ms |
119924 KB |
Output is correct |
9 |
Correct |
104 ms |
111436 KB |
Output is correct |
10 |
Correct |
85 ms |
108116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
59852 KB |
Output is correct |
2 |
Correct |
28 ms |
59484 KB |
Output is correct |
3 |
Correct |
30 ms |
59652 KB |
Output is correct |
4 |
Correct |
25 ms |
59608 KB |
Output is correct |
5 |
Correct |
27 ms |
59712 KB |
Output is correct |
6 |
Correct |
33 ms |
59608 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
58204 KB |
Output is correct |
2 |
Correct |
13 ms |
58312 KB |
Output is correct |
3 |
Correct |
15 ms |
58204 KB |
Output is correct |
4 |
Correct |
14 ms |
58204 KB |
Output is correct |
5 |
Correct |
14 ms |
58204 KB |
Output is correct |
6 |
Correct |
14 ms |
58204 KB |
Output is correct |
7 |
Correct |
14 ms |
58340 KB |
Output is correct |
8 |
Correct |
140 ms |
156668 KB |
Output is correct |
9 |
Correct |
141 ms |
151760 KB |
Output is correct |
10 |
Correct |
75 ms |
113756 KB |
Output is correct |
11 |
Correct |
65 ms |
111368 KB |
Output is correct |
12 |
Correct |
117 ms |
143188 KB |
Output is correct |
13 |
Correct |
117 ms |
144468 KB |
Output is correct |
14 |
Correct |
53 ms |
74088 KB |
Output is correct |
15 |
Correct |
114 ms |
119924 KB |
Output is correct |
16 |
Correct |
104 ms |
111436 KB |
Output is correct |
17 |
Correct |
85 ms |
108116 KB |
Output is correct |
18 |
Correct |
36 ms |
59852 KB |
Output is correct |
19 |
Correct |
28 ms |
59484 KB |
Output is correct |
20 |
Correct |
30 ms |
59652 KB |
Output is correct |
21 |
Correct |
25 ms |
59608 KB |
Output is correct |
22 |
Correct |
27 ms |
59712 KB |
Output is correct |
23 |
Correct |
33 ms |
59608 KB |
Output is correct |
24 |
Correct |
157 ms |
140404 KB |
Output is correct |
25 |
Correct |
158 ms |
140372 KB |
Output is correct |
26 |
Correct |
151 ms |
139268 KB |
Output is correct |
27 |
Correct |
73 ms |
106084 KB |
Output is correct |
28 |
Correct |
132 ms |
84184 KB |
Output is correct |
29 |
Correct |
75 ms |
67272 KB |
Output is correct |