#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> ii;
const int N = 1e5 + 5,
INF = 1e9;
int n, m;
string s[N];
int id(char x){
if(x == 'A') return 0;
if(x == 'C') return 1;
if(x == 'G') return 2;
if(x == 'U') return 3;
}
struct Trie1{
struct node{
node* child[4];
int l, r;
node(): l(INF), r(-INF) {
child[0] = child[1] = child[2] = child[3] = NULL;
}
};
node* root;
Trie1(): root(new node()){}
void add(string &s, int idx){
auto p = root;
for(auto ch : s){
int c = id(ch);
if(!p->child[c]) p->child[c] = new node();
p = p->child[c];
p->l = min(p->l, idx);
p->r = max(p->r, idx);
}
}
ii query(string &s){
auto p = root;
for(auto ch : s){
int c = id(ch);
if(!p->child[c]) return {-1, -1};
p = p->child[c];
}
return {p->l, p->r};
}
} d1;
struct Trie2{
struct node{
node* child[4];
vector<int> pos;
node(){
child[0] = child[1] = child[2] = child[3] = NULL;
}
};
node* root;
Trie2(): root(new node()) {}
void add(string &s, int idx){
auto p = root;
for(auto ch : s){
int c = id(ch);
if(!p->child[c]) p->child[c] = new node();
p = p->child[c];
p->pos.push_back(idx);
}
}
int query(string &s, int l, int r){
auto p = root;
for(auto ch : s){
int c = id(ch);
if(!p->child[c]) return 0;
p = p->child[c];
}
int tl = lower_bound(p->pos.begin(), p->pos.end(), l) - p->pos.begin(),
tr = upper_bound(p->pos.begin(), p->pos.end(), r) - p->pos.begin() - 1;
return tr - tl + 1;
}
} d2;
int32_t main(){
cin.tie(0)->sync_with_stdio(0);
// #define task "test"
// if(fopen(task ".inp", "r")){
// freopen(task ".inp", "r", stdin);
// freopen(task ".out", "w", stdout);
// }
cin >> n >> m;
for(int i = 1; i <= n; i++) cin >> s[i];
sort(s + 1, s + n + 1);
for(int i = 1; i <= n; i++){
d1.add(s[i], i);
reverse(s[i].begin(), s[i].end());
d2.add(s[i], i);
}
while(m--){
string p, q; cin >> p >> q;
reverse(q.begin(), q.end());
auto range = d1.query(p);
if(range == make_pair(-1, -1)) cout << 0 << "\n";
else{
cout << d2.query(q, range.first, range.second) << "\n";
}
}
return 0;
}
Compilation message
selling_rna.cpp: In function 'int id(char)':
selling_rna.cpp:18:1: warning: control reaches end of non-void function [-Wreturn-type]
18 | }
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3408 KB |
Output is correct |
2 |
Correct |
2 ms |
3408 KB |
Output is correct |
3 |
Correct |
2 ms |
3408 KB |
Output is correct |
4 |
Correct |
2 ms |
3408 KB |
Output is correct |
5 |
Correct |
2 ms |
3576 KB |
Output is correct |
6 |
Correct |
2 ms |
3520 KB |
Output is correct |
7 |
Correct |
2 ms |
3408 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
239 ms |
193612 KB |
Output is correct |
2 |
Correct |
212 ms |
180552 KB |
Output is correct |
3 |
Correct |
128 ms |
110372 KB |
Output is correct |
4 |
Correct |
121 ms |
105544 KB |
Output is correct |
5 |
Correct |
216 ms |
178772 KB |
Output is correct |
6 |
Correct |
220 ms |
180552 KB |
Output is correct |
7 |
Correct |
46 ms |
19132 KB |
Output is correct |
8 |
Correct |
144 ms |
117840 KB |
Output is correct |
9 |
Correct |
118 ms |
95560 KB |
Output is correct |
10 |
Correct |
102 ms |
95820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
4556 KB |
Output is correct |
2 |
Correct |
13 ms |
4924 KB |
Output is correct |
3 |
Correct |
16 ms |
4724 KB |
Output is correct |
4 |
Correct |
12 ms |
4432 KB |
Output is correct |
5 |
Correct |
14 ms |
4688 KB |
Output is correct |
6 |
Correct |
17 ms |
4688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3408 KB |
Output is correct |
2 |
Correct |
2 ms |
3408 KB |
Output is correct |
3 |
Correct |
2 ms |
3408 KB |
Output is correct |
4 |
Correct |
2 ms |
3408 KB |
Output is correct |
5 |
Correct |
2 ms |
3576 KB |
Output is correct |
6 |
Correct |
2 ms |
3520 KB |
Output is correct |
7 |
Correct |
2 ms |
3408 KB |
Output is correct |
8 |
Correct |
239 ms |
193612 KB |
Output is correct |
9 |
Correct |
212 ms |
180552 KB |
Output is correct |
10 |
Correct |
128 ms |
110372 KB |
Output is correct |
11 |
Correct |
121 ms |
105544 KB |
Output is correct |
12 |
Correct |
216 ms |
178772 KB |
Output is correct |
13 |
Correct |
220 ms |
180552 KB |
Output is correct |
14 |
Correct |
46 ms |
19132 KB |
Output is correct |
15 |
Correct |
144 ms |
117840 KB |
Output is correct |
16 |
Correct |
118 ms |
95560 KB |
Output is correct |
17 |
Correct |
102 ms |
95820 KB |
Output is correct |
18 |
Correct |
16 ms |
4556 KB |
Output is correct |
19 |
Correct |
13 ms |
4924 KB |
Output is correct |
20 |
Correct |
16 ms |
4724 KB |
Output is correct |
21 |
Correct |
12 ms |
4432 KB |
Output is correct |
22 |
Correct |
14 ms |
4688 KB |
Output is correct |
23 |
Correct |
17 ms |
4688 KB |
Output is correct |
24 |
Correct |
190 ms |
158816 KB |
Output is correct |
25 |
Correct |
201 ms |
160840 KB |
Output is correct |
26 |
Correct |
195 ms |
158600 KB |
Output is correct |
27 |
Correct |
112 ms |
91992 KB |
Output is correct |
28 |
Correct |
112 ms |
38156 KB |
Output is correct |
29 |
Correct |
46 ms |
11980 KB |
Output is correct |