#include <bits/stdc++.h>
using namespace std;
int c[256];
class trie_t {
public:
int l, r, is_sorted;
vector<int> index;
trie_t* g[4];
trie_t() {
l = 1e9, r = -1, is_sorted = 0;
for (int i = 0; i < 4; i++) g[i] = nullptr;
}
static void add(trie_t* root, const string& s, int id) {
for (auto&& x : s) {
if (!root->g[c[x]]) {
root->g[c[x]] = new trie_t();
}
root = root->g[c[x]];
root->index.emplace_back(id);
root->l = min(root->l, id);
root->r = max(root->r, id);
}
}
static pair<int, int> get(trie_t* root, const string& s) {
for (auto&& x : s) {
if (!root->g[c[x]]) {
return {-1, -1};
}
root = root->g[c[x]];
}
return {root->l, root->r};
}
static int solve(trie_t* root, int l, int r, const string& s) {
for (auto&& x : s) {
if (!root->g[c[x]]) {
return 0;
}
root = root->g[c[x]];
}
if (!root->is_sorted) sort(root->index.begin(), root->index.end()), root->is_sorted = 1;
return upper_bound(root->index.begin(), root->index.end(), r) - lower_bound(root->index.begin(), root->index.end(), l);
}
};
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
c['A'] = 0, c['G'] = 1, c['C'] = 2, c['U'] = 3;
int n, m;
cin >> n >> m;
vector<string> a(n), b(m);
for (int i = 0; i < n; i++) cin >> a[i];
sort(a.begin(), a.end());
trie_t* tree = new trie_t();
trie_t* rev_tree = new trie_t();
for (int i = 0; i < n; i++) trie_t::add(tree, a[i], i);
for (int i = 0; i < n; i++) reverse(a[i].begin(), a[i].end());
for (int i = 0; i < n; i++) trie_t::add(rev_tree, a[i], i);
for (int i = 0; i < m; i++) {
string p, q;
cin >> p >> q;
reverse(q.begin(), q.end());
auto [l, r] = trie_t::get(tree, p);
cout << (l <= r ? trie_t::solve(rev_tree, l, r, q) : 0) << '\n';
}
}
Compilation message
selling_rna.cpp: In static member function 'static void trie_t::add(trie_t*, const string&, int)':
selling_rna.cpp:18:40: warning: array subscript has type 'char' [-Wchar-subscripts]
18 | if (!root->g[c[x]]) {
| ^
selling_rna.cpp:19:43: warning: array subscript has type 'char' [-Wchar-subscripts]
19 | root->g[c[x]] = new trie_t();
| ^
selling_rna.cpp:21:42: warning: array subscript has type 'char' [-Wchar-subscripts]
21 | root = root->g[c[x]];
| ^
selling_rna.cpp: In static member function 'static std::pair<int, int> trie_t::get(trie_t*, const string&)':
selling_rna.cpp:30:40: warning: array subscript has type 'char' [-Wchar-subscripts]
30 | if (!root->g[c[x]]) {
| ^
selling_rna.cpp:33:42: warning: array subscript has type 'char' [-Wchar-subscripts]
33 | root = root->g[c[x]];
| ^
selling_rna.cpp: In static member function 'static int trie_t::solve(trie_t*, int, int, const string&)':
selling_rna.cpp:40:40: warning: array subscript has type 'char' [-Wchar-subscripts]
40 | if (!root->g[c[x]]) {
| ^
selling_rna.cpp:43:42: warning: array subscript has type 'char' [-Wchar-subscripts]
43 | root = root->g[c[x]];
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
180 ms |
233664 KB |
Output is correct |
2 |
Correct |
182 ms |
221424 KB |
Output is correct |
3 |
Correct |
181 ms |
230692 KB |
Output is correct |
4 |
Correct |
177 ms |
219888 KB |
Output is correct |
5 |
Correct |
209 ms |
271132 KB |
Output is correct |
6 |
Correct |
211 ms |
275244 KB |
Output is correct |
7 |
Correct |
85 ms |
24652 KB |
Output is correct |
8 |
Correct |
193 ms |
177136 KB |
Output is correct |
9 |
Correct |
166 ms |
150604 KB |
Output is correct |
10 |
Correct |
141 ms |
144716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
5244 KB |
Output is correct |
2 |
Correct |
16 ms |
4192 KB |
Output is correct |
3 |
Correct |
19 ms |
4620 KB |
Output is correct |
4 |
Correct |
20 ms |
3948 KB |
Output is correct |
5 |
Correct |
17 ms |
4148 KB |
Output is correct |
6 |
Correct |
20 ms |
4624 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
324 KB |
Output is correct |
8 |
Correct |
180 ms |
233664 KB |
Output is correct |
9 |
Correct |
182 ms |
221424 KB |
Output is correct |
10 |
Correct |
181 ms |
230692 KB |
Output is correct |
11 |
Correct |
177 ms |
219888 KB |
Output is correct |
12 |
Correct |
209 ms |
271132 KB |
Output is correct |
13 |
Correct |
211 ms |
275244 KB |
Output is correct |
14 |
Correct |
85 ms |
24652 KB |
Output is correct |
15 |
Correct |
193 ms |
177136 KB |
Output is correct |
16 |
Correct |
166 ms |
150604 KB |
Output is correct |
17 |
Correct |
141 ms |
144716 KB |
Output is correct |
18 |
Correct |
20 ms |
5244 KB |
Output is correct |
19 |
Correct |
16 ms |
4192 KB |
Output is correct |
20 |
Correct |
19 ms |
4620 KB |
Output is correct |
21 |
Correct |
20 ms |
3948 KB |
Output is correct |
22 |
Correct |
17 ms |
4148 KB |
Output is correct |
23 |
Correct |
20 ms |
4624 KB |
Output is correct |
24 |
Correct |
168 ms |
193868 KB |
Output is correct |
25 |
Correct |
164 ms |
195280 KB |
Output is correct |
26 |
Correct |
181 ms |
191532 KB |
Output is correct |
27 |
Correct |
177 ms |
191552 KB |
Output is correct |
28 |
Correct |
127 ms |
54256 KB |
Output is correct |
29 |
Correct |
58 ms |
19864 KB |
Output is correct |