#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int getval(char s){
if (s == 'A') return 0;
else if (s == 'G') return 1;
else if (s == 'C') return 2;
else return 3;
}
char getchar(int c){
if (c == 0) return 'A';
else if (c == 1) return 'G';
else if (c == 2) return 'C';
else if (c == 3) return 'U';
}
struct Trie{
struct Node{
int l, r;
Node *a[4];
Node(){
l = (int)1e9;
r = (int)-1e9;
for (int i = 0; i <= 3; ++i)
a[i] = NULL;
}
};
Node *root = new Node();
void add(string &s, int id){
Node *p = root;
for (int i = 0; i < s.size(); ++i){
int c = getval(s[i]);
if (p->a[c] == NULL) p->a[c] = new Node();
p = p->a[c];
p->l = min(p->l, id);
p->r = max(p->r, id);
}
}
pair<int, int> getrange(string &s){
Node *p = root;
for (int i = 0; i < s.size(); ++i){
int c = getval(s[i]);
if (p->a[c] == NULL) return {0, 0};
p = p->a[c];
}
return {p->l, p->r};
}
};
struct RevTrie{
struct Node{
Node *a[4];
vector<int> h;
Node(){
for (int i = 0; i <= 3; ++i)
a[i] = NULL;
}
};
Node *root = new Node();
void add(string &s, int id){
reverse(s.begin(), s.end());
Node *p = root;
for (int i = 0; i < s.size(); ++i){
int c = getval(s[i]);
if (p->a[c] == NULL) p->a[c] = new Node();
p = p->a[c];
p->h.push_back(id);
}
}
int match(string &s, pair<int, int> range){
reverse(s.begin(), s.end());
Node *p = root;
for (int i = 0; i < s.size(); ++i){
int c = getval(s[i]);
if (p->a[c] == NULL) return 0;
p = p->a[c];
}
int small = lower_bound(p->h.begin(), p->h.end(), range.first) - p->h.begin() - 1;
int large = upper_bound(p->h.begin(), p->h.end(), range.second) - p->h.begin() - 1;
return large - small;
}
};
int n, m;
string s[N], p, q;
Trie trie1;
RevTrie trie2;
int main(){
cin.tie(0)->sync_with_stdio(0);
cin >> n >> m;
for (int i = 1; i <= n; ++i){
cin >> s[i];
}
sort(s + 1, s + 1 + n);
for (int i = 1; i <= n; ++i){
trie1.add(s[i], i);
trie2.add(s[i], i);
}
while(m--){
cin >> p >> q;
cout << trie2.match(q, trie1.getrange(p)) << "\n";
}
return 0;
}
Compilation message
selling_rna.cpp: In member function 'void Trie::add(std::string&, int)':
selling_rna.cpp:30:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
30 | for (int i = 0; i < s.size(); ++i){
| ~~^~~~~~~~~~
selling_rna.cpp: In member function 'std::pair<int, int> Trie::getrange(std::string&)':
selling_rna.cpp:40:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | for (int i = 0; i < s.size(); ++i){
| ~~^~~~~~~~~~
selling_rna.cpp: In member function 'void RevTrie::add(std::string&, int)':
selling_rna.cpp:61:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
61 | for (int i = 0; i < s.size(); ++i){
| ~~^~~~~~~~~~
selling_rna.cpp: In member function 'int RevTrie::match(std::string&, std::pair<int, int>)':
selling_rna.cpp:71:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
71 | for (int i = 0; i < s.size(); ++i){
| ~~^~~~~~~~~~
selling_rna.cpp: In function 'char getchar(int)':
selling_rna.cpp:15:1: warning: control reaches end of non-void function [-Wreturn-type]
15 | }
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3420 KB |
Output is correct |
2 |
Correct |
1 ms |
3420 KB |
Output is correct |
3 |
Correct |
1 ms |
3420 KB |
Output is correct |
4 |
Correct |
2 ms |
3420 KB |
Output is correct |
5 |
Correct |
2 ms |
3420 KB |
Output is correct |
6 |
Correct |
2 ms |
3572 KB |
Output is correct |
7 |
Correct |
1 ms |
3420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
219 ms |
190360 KB |
Output is correct |
2 |
Correct |
194 ms |
180560 KB |
Output is correct |
3 |
Correct |
132 ms |
108060 KB |
Output is correct |
4 |
Correct |
128 ms |
102992 KB |
Output is correct |
5 |
Correct |
180 ms |
176436 KB |
Output is correct |
6 |
Correct |
189 ms |
178744 KB |
Output is correct |
7 |
Correct |
56 ms |
13648 KB |
Output is correct |
8 |
Correct |
162 ms |
111952 KB |
Output is correct |
9 |
Correct |
131 ms |
95536 KB |
Output is correct |
10 |
Correct |
98 ms |
92696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
4488 KB |
Output is correct |
2 |
Correct |
14 ms |
4444 KB |
Output is correct |
3 |
Correct |
17 ms |
4416 KB |
Output is correct |
4 |
Correct |
19 ms |
4184 KB |
Output is correct |
5 |
Correct |
16 ms |
4440 KB |
Output is correct |
6 |
Correct |
18 ms |
4356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3420 KB |
Output is correct |
2 |
Correct |
1 ms |
3420 KB |
Output is correct |
3 |
Correct |
1 ms |
3420 KB |
Output is correct |
4 |
Correct |
2 ms |
3420 KB |
Output is correct |
5 |
Correct |
2 ms |
3420 KB |
Output is correct |
6 |
Correct |
2 ms |
3572 KB |
Output is correct |
7 |
Correct |
1 ms |
3420 KB |
Output is correct |
8 |
Correct |
219 ms |
190360 KB |
Output is correct |
9 |
Correct |
194 ms |
180560 KB |
Output is correct |
10 |
Correct |
132 ms |
108060 KB |
Output is correct |
11 |
Correct |
128 ms |
102992 KB |
Output is correct |
12 |
Correct |
180 ms |
176436 KB |
Output is correct |
13 |
Correct |
189 ms |
178744 KB |
Output is correct |
14 |
Correct |
56 ms |
13648 KB |
Output is correct |
15 |
Correct |
162 ms |
111952 KB |
Output is correct |
16 |
Correct |
131 ms |
95536 KB |
Output is correct |
17 |
Correct |
98 ms |
92696 KB |
Output is correct |
18 |
Correct |
18 ms |
4488 KB |
Output is correct |
19 |
Correct |
14 ms |
4444 KB |
Output is correct |
20 |
Correct |
17 ms |
4416 KB |
Output is correct |
21 |
Correct |
19 ms |
4184 KB |
Output is correct |
22 |
Correct |
16 ms |
4440 KB |
Output is correct |
23 |
Correct |
18 ms |
4356 KB |
Output is correct |
24 |
Correct |
195 ms |
156672 KB |
Output is correct |
25 |
Correct |
183 ms |
156796 KB |
Output is correct |
26 |
Correct |
181 ms |
154740 KB |
Output is correct |
27 |
Correct |
111 ms |
90196 KB |
Output is correct |
28 |
Correct |
119 ms |
34328 KB |
Output is correct |
29 |
Correct |
52 ms |
8852 KB |
Output is correct |