#include <bits/stdc++.h>
using namespace std;
struct T {
int nxt[4] = {-1, -1, -1, -1};
vector<int> id;
};
int mapuj(char x)
{
if(x == 'A') {
return 0;
}
if(x == 'G') {
return 1;
}
if(x == 'C') {
return 2;
}
if(x == 'U') {
return 3;
}
}
struct tree {
vector<T> trie;
tree() { trie.emplace_back(); }
void insert(string &s, int i)
{
int v = 0;
for(char x : s) {
int id = mapuj(x);
if(trie[v].nxt[id] == -1) {
trie[v].nxt[id] = (int)trie.size();
trie.emplace_back();
}
trie[v].id.emplace_back(i);
v = trie[v].nxt[id];
}
trie[v].id.emplace_back(i);
}
int search(string &s)
{
int v = 0;
for(char x : s) {
int id = mapuj(x);
if(trie[v].nxt[id] == -1) {
return -1;
}
v = trie[v].nxt[id];
}
return v;
}
};
int32_t main()
{
cin.tie(0)->sync_with_stdio(0);
int n, q;
cin >> n >> q;
vector<string> s(n + 1);
for(int i = 1; i <= n; i++) {
cin >> s[i];
}
sort(1 + s.begin(), s.end());
tree trie, trierev;
for(int i = 1; i <= n; ++i) {
trie.insert(s[i], i);
reverse(s[i].begin(), s[i].end());
trierev.insert(s[i], i);
}
for(int i = 1; i <= q; i++) {
string pre, suf;
cin >> pre >> suf;
int one = trie.search(pre);
reverse(suf.begin(), suf.end());
int two = trierev.search(suf);
if(one == -1 || two == -1) {
cout << 0 << '\n';
continue;
}
int lo = trie.trie[one].id.front(), hi = trie.trie[one].id.back();
auto it1 = lower_bound(trierev.trie[two].id.begin(), trierev.trie[two].id.end(), lo);
auto it2 = upper_bound(trierev.trie[two].id.begin(), trierev.trie[two].id.end(), hi);
if(it1 == trierev.trie[two].id.end() || hi < *it1) {
cout << 0 << '\n';
continue;
}
cout << distance(it1, it2) << '\n';
}
}
Compilation message
selling_rna.cpp: In function 'int mapuj(char)':
selling_rna.cpp:24:1: warning: control reaches end of non-void function [-Wreturn-type]
24 | }
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 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 |
320 KB |
Output is correct |
7 |
Correct |
1 ms |
324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
251 ms |
156520 KB |
Output is correct |
2 |
Correct |
309 ms |
148460 KB |
Output is correct |
3 |
Correct |
255 ms |
154180 KB |
Output is correct |
4 |
Correct |
271 ms |
147288 KB |
Output is correct |
5 |
Correct |
299 ms |
192264 KB |
Output is correct |
6 |
Correct |
311 ms |
192368 KB |
Output is correct |
7 |
Correct |
80 ms |
24348 KB |
Output is correct |
8 |
Correct |
276 ms |
131888 KB |
Output is correct |
9 |
Correct |
228 ms |
115120 KB |
Output is correct |
10 |
Correct |
198 ms |
110888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
4164 KB |
Output is correct |
2 |
Correct |
38 ms |
3228 KB |
Output is correct |
3 |
Correct |
27 ms |
3732 KB |
Output is correct |
4 |
Correct |
21 ms |
3268 KB |
Output is correct |
5 |
Correct |
32 ms |
3240 KB |
Output is correct |
6 |
Correct |
39 ms |
3680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 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 |
320 KB |
Output is correct |
7 |
Correct |
1 ms |
324 KB |
Output is correct |
8 |
Correct |
251 ms |
156520 KB |
Output is correct |
9 |
Correct |
309 ms |
148460 KB |
Output is correct |
10 |
Correct |
255 ms |
154180 KB |
Output is correct |
11 |
Correct |
271 ms |
147288 KB |
Output is correct |
12 |
Correct |
299 ms |
192264 KB |
Output is correct |
13 |
Correct |
311 ms |
192368 KB |
Output is correct |
14 |
Correct |
80 ms |
24348 KB |
Output is correct |
15 |
Correct |
276 ms |
131888 KB |
Output is correct |
16 |
Correct |
228 ms |
115120 KB |
Output is correct |
17 |
Correct |
198 ms |
110888 KB |
Output is correct |
18 |
Correct |
25 ms |
4164 KB |
Output is correct |
19 |
Correct |
38 ms |
3228 KB |
Output is correct |
20 |
Correct |
27 ms |
3732 KB |
Output is correct |
21 |
Correct |
21 ms |
3268 KB |
Output is correct |
22 |
Correct |
32 ms |
3240 KB |
Output is correct |
23 |
Correct |
39 ms |
3680 KB |
Output is correct |
24 |
Correct |
277 ms |
130336 KB |
Output is correct |
25 |
Correct |
297 ms |
130704 KB |
Output is correct |
26 |
Correct |
292 ms |
129104 KB |
Output is correct |
27 |
Correct |
235 ms |
128672 KB |
Output is correct |
28 |
Correct |
191 ms |
43536 KB |
Output is correct |
29 |
Correct |
90 ms |
17472 KB |
Output is correct |