#include <bits/stdc++.h>
using namespace std;
const int N = 100100;
int to(char c) {
if (c == 'A') return 0;
else if (c == 'C') return 1;
else if (c == 'G') return 2;
else return 3;
}
int n, q;
string s[N];
int tot;
int l[275 * N];
int r[275 * N];
int sz[275 * N];
int nxt[275 * N][4];
int root;
int T[N << 2];
int nxtNode() {
return ++tot;
}
void add(int root, string s, int id = 0) {
int cur = root;
for (char c : s) {
c = (int) to(c);
if (!nxt[cur][c]) {
nxt[cur][c] = nxtNode();
}
cur = nxt[cur][c];
if (l[cur] == 0) {
l[cur] = id;
}
r[cur] = id;
++sz[cur];
}
}
int getNode(int root, string s) {
int cur = root;
for (char c : s) {
c = (int) to(c);
if (!nxt[cur][c]) {
return 0;
}
cur = nxt[cur][c];
}
return cur;
}
#define mid ((l + r) >> 1)
void build(int v, int l, int r) {
T[v] = nxtNode();
for (int i = l; i <= r; ++i) {
add(T[v], s[i]);
}
if (l < r) {
build(v << 1, l, mid);
build(v << 1 | 1, mid + 1, r);
}
}
int getCount(int v, int l, int r, int L, int R, string &s) {
if (L <= l && r <= R) {
return sz[getNode(T[v], s)];
}
int ans = 0;
if (L <= mid) ans += getCount(v << 1, l, mid, L, R, s);
if (mid < R) ans += getCount(v << 1 | 1, mid + 1, r, L, R, s);
return ans;
}
int solve(string p, string q) {
int cur = getNode(root, p);
if (!cur) return 0;
reverse(q.begin(), q.end());
return getCount(1, 1, n, l[cur], r[cur], q);
}
int main() {
ios_base::sync_with_stdio(false);
cin >> n >> q;
for (int i = 1; i <= n; ++i) {
cin >> s[i];
}
sort(s + 1, s + 1 + n);
root = nxtNode();
for (int i = 1; i <= n; ++i) {
add(root, s[i], i);
reverse(s[i].begin(), s[i].end());
}
build(1, 1, n);
while (q--) {
string u, v;
cin >> u >> v;
cout << solve(u, v) << '\n';
}
}
Compilation message
selling_rna.cpp: In function 'void add(int, std::__cxx11::string, int)':
selling_rna.cpp:32:22: warning: array subscript has type 'char' [-Wchar-subscripts]
if (!nxt[cur][c]) {
^
selling_rna.cpp:33:20: warning: array subscript has type 'char' [-Wchar-subscripts]
nxt[cur][c] = nxtNode();
^
selling_rna.cpp:35:23: warning: array subscript has type 'char' [-Wchar-subscripts]
cur = nxt[cur][c];
^
selling_rna.cpp: In function 'int getNode(int, std::__cxx11::string)':
selling_rna.cpp:48:22: warning: array subscript has type 'char' [-Wchar-subscripts]
if (!nxt[cur][c]) {
^
selling_rna.cpp:51:23: warning: array subscript has type 'char' [-Wchar-subscripts]
cur = nxt[cur][c];
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
3584 KB |
Output is correct |
2 |
Correct |
5 ms |
3584 KB |
Output is correct |
3 |
Correct |
4 ms |
3584 KB |
Output is correct |
4 |
Correct |
5 ms |
3584 KB |
Output is correct |
5 |
Correct |
5 ms |
3584 KB |
Output is correct |
6 |
Correct |
4 ms |
3584 KB |
Output is correct |
7 |
Correct |
5 ms |
3584 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
771 ms |
653144 KB |
Output is correct |
2 |
Correct |
842 ms |
696208 KB |
Output is correct |
3 |
Correct |
466 ms |
231160 KB |
Output is correct |
4 |
Correct |
429 ms |
225988 KB |
Output is correct |
5 |
Correct |
648 ms |
489148 KB |
Output is correct |
6 |
Correct |
673 ms |
496368 KB |
Output is correct |
7 |
Correct |
210 ms |
87808 KB |
Output is correct |
8 |
Correct |
653 ms |
477824 KB |
Output is correct |
9 |
Correct |
557 ms |
421232 KB |
Output is correct |
10 |
Correct |
435 ms |
313080 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
123 ms |
12488 KB |
Output is correct |
2 |
Correct |
90 ms |
13304 KB |
Output is correct |
3 |
Correct |
108 ms |
13148 KB |
Output is correct |
4 |
Correct |
88 ms |
11512 KB |
Output is correct |
5 |
Correct |
86 ms |
11996 KB |
Output is correct |
6 |
Correct |
107 ms |
12664 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
3584 KB |
Output is correct |
2 |
Correct |
5 ms |
3584 KB |
Output is correct |
3 |
Correct |
4 ms |
3584 KB |
Output is correct |
4 |
Correct |
5 ms |
3584 KB |
Output is correct |
5 |
Correct |
5 ms |
3584 KB |
Output is correct |
6 |
Correct |
4 ms |
3584 KB |
Output is correct |
7 |
Correct |
5 ms |
3584 KB |
Output is correct |
8 |
Correct |
771 ms |
653144 KB |
Output is correct |
9 |
Correct |
842 ms |
696208 KB |
Output is correct |
10 |
Correct |
466 ms |
231160 KB |
Output is correct |
11 |
Correct |
429 ms |
225988 KB |
Output is correct |
12 |
Correct |
648 ms |
489148 KB |
Output is correct |
13 |
Correct |
673 ms |
496368 KB |
Output is correct |
14 |
Correct |
210 ms |
87808 KB |
Output is correct |
15 |
Correct |
653 ms |
477824 KB |
Output is correct |
16 |
Correct |
557 ms |
421232 KB |
Output is correct |
17 |
Correct |
435 ms |
313080 KB |
Output is correct |
18 |
Correct |
123 ms |
12488 KB |
Output is correct |
19 |
Correct |
90 ms |
13304 KB |
Output is correct |
20 |
Correct |
108 ms |
13148 KB |
Output is correct |
21 |
Correct |
88 ms |
11512 KB |
Output is correct |
22 |
Correct |
86 ms |
11996 KB |
Output is correct |
23 |
Correct |
107 ms |
12664 KB |
Output is correct |
24 |
Correct |
821 ms |
663508 KB |
Output is correct |
25 |
Correct |
1029 ms |
663672 KB |
Output is correct |
26 |
Correct |
801 ms |
657620 KB |
Output is correct |
27 |
Correct |
450 ms |
214364 KB |
Output is correct |
28 |
Correct |
691 ms |
216800 KB |
Output is correct |
29 |
Correct |
403 ms |
65728 KB |
Output is correct |