#include <bits/stdc++.h>
#define ii pair<int, int>
#define fi first
#define se second
#define inf 0x3f3f3f3f
using namespace std;
using ll = long long;
const int nx=1e5+5;
const int N=2e6+5;
int getid(char c)
{
if(c=='A')
return 0;
if(c=='C')
return 1;
if(c=='G')
return 2;
return 3;
}
int n, m;
string a[nx];
struct trie
{
struct dak
{
int child[4], l, r;
dak()
{
memset(child, -1, sizeof(child));
l=inf, r=0;
}
} nod[N<<2];
int cur;
trie() : cur(0){};
int new_node()
{
cur++;
return cur;
}
void add(string s, int id)
{
int pos=0;
for(int i = 0; i < s.size(); i++)
{
int c=getid(s[i]);
if(nod[pos].child[c]==-1)
nod[pos].child[c]=new_node();
pos=nod[pos].child[c];
nod[pos].l=min(nod[pos].l, id);
nod[pos].r=max(nod[pos].r, id);
}
}
ii get_range(string s)
{
int pos=0;
for(int i = 0; i < s.size(); i++)
{
int c=getid(s[i]);
if(nod[pos].child[c]==-1)
return ii(-1, -1);
pos=nod[pos].child[c];
}
return ii(nod[pos].l, nod[pos].r);
}
} T;
struct rev_trie
{
struct dak
{
int child[4];
vector<int> ids;
dak()
{
memset(child, -1, sizeof(child));
ids.clear();
}
} nod[N<<2];
int cur;
rev_trie() : cur(0){};
int new_node()
{
cur++;
return cur;
}
void add(string s, int id)
{
int pos=0;
for(int i = s.size()-1; i >= 0; i--)
{
int c=getid(s[i]);
if(nod[pos].child[c]==-1)
nod[pos].child[c]=new_node();
pos=nod[pos].child[c];
nod[pos].ids.emplace_back(id);
}
}
int query(string s, ii f)
{
int pos=0;
for(int i = s.size()-1; i >= 0; i--)
{
int c=getid(s[i]);
if(nod[pos].child[c]==-1)
return 0;
pos=nod[pos].child[c];
}
int l=lower_bound(nod[pos].ids.begin(), nod[pos].ids.end(), f.fi)-nod[pos].ids.begin();
int r=upper_bound(nod[pos].ids.begin(), nod[pos].ids.end(), f.se)-nod[pos].ids.begin();
return r-l;
}
} T1;
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin>>n>>m;
for(int i = 1; i <= n; i++)
cin>>a[i];
sort(a+1, a+n+1);
for(int i = 1; i <= n; i++)
{
T.add(a[i], i);
T1.add(a[i], i);
}
while(m--)
{
string x, y;
cin>>x>>y;
ii cur=T.get_range(x);
cout<<T1.query(y, cur)<<'\n';
}
}
Compilation message
selling_rna.cpp: In member function 'void trie::add(std::string, int)':
selling_rna.cpp:43:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
43 | for(int i = 0; i < s.size(); i++)
| ~~^~~~~~~~~~
selling_rna.cpp: In member function 'std::pair<int, int> trie::get_range(std::string)':
selling_rna.cpp:56:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
56 | for(int i = 0; i < s.size(); i++)
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
177 ms |
504464 KB |
Output is correct |
2 |
Correct |
115 ms |
504400 KB |
Output is correct |
3 |
Correct |
114 ms |
504400 KB |
Output is correct |
4 |
Correct |
115 ms |
504424 KB |
Output is correct |
5 |
Correct |
116 ms |
504672 KB |
Output is correct |
6 |
Correct |
113 ms |
504520 KB |
Output is correct |
7 |
Correct |
113 ms |
504404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
256 ms |
572076 KB |
Output is correct |
2 |
Correct |
235 ms |
568724 KB |
Output is correct |
3 |
Correct |
155 ms |
521712 KB |
Output is correct |
4 |
Correct |
154 ms |
521488 KB |
Output is correct |
5 |
Correct |
178 ms |
546568 KB |
Output is correct |
6 |
Correct |
183 ms |
546992 KB |
Output is correct |
7 |
Correct |
146 ms |
520056 KB |
Output is correct |
8 |
Correct |
209 ms |
539984 KB |
Output is correct |
9 |
Correct |
195 ms |
536156 KB |
Output is correct |
10 |
Correct |
168 ms |
533780 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
124 ms |
505932 KB |
Output is correct |
2 |
Correct |
121 ms |
505624 KB |
Output is correct |
3 |
Correct |
122 ms |
505676 KB |
Output is correct |
4 |
Correct |
122 ms |
505552 KB |
Output is correct |
5 |
Correct |
122 ms |
506012 KB |
Output is correct |
6 |
Correct |
130 ms |
505844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
177 ms |
504464 KB |
Output is correct |
2 |
Correct |
115 ms |
504400 KB |
Output is correct |
3 |
Correct |
114 ms |
504400 KB |
Output is correct |
4 |
Correct |
115 ms |
504424 KB |
Output is correct |
5 |
Correct |
116 ms |
504672 KB |
Output is correct |
6 |
Correct |
113 ms |
504520 KB |
Output is correct |
7 |
Correct |
113 ms |
504404 KB |
Output is correct |
8 |
Correct |
256 ms |
572076 KB |
Output is correct |
9 |
Correct |
235 ms |
568724 KB |
Output is correct |
10 |
Correct |
155 ms |
521712 KB |
Output is correct |
11 |
Correct |
154 ms |
521488 KB |
Output is correct |
12 |
Correct |
178 ms |
546568 KB |
Output is correct |
13 |
Correct |
183 ms |
546992 KB |
Output is correct |
14 |
Correct |
146 ms |
520056 KB |
Output is correct |
15 |
Correct |
209 ms |
539984 KB |
Output is correct |
16 |
Correct |
195 ms |
536156 KB |
Output is correct |
17 |
Correct |
168 ms |
533780 KB |
Output is correct |
18 |
Correct |
124 ms |
505932 KB |
Output is correct |
19 |
Correct |
121 ms |
505624 KB |
Output is correct |
20 |
Correct |
122 ms |
505676 KB |
Output is correct |
21 |
Correct |
122 ms |
505552 KB |
Output is correct |
22 |
Correct |
122 ms |
506012 KB |
Output is correct |
23 |
Correct |
130 ms |
505844 KB |
Output is correct |
24 |
Correct |
222 ms |
561176 KB |
Output is correct |
25 |
Correct |
240 ms |
561388 KB |
Output is correct |
26 |
Correct |
217 ms |
560212 KB |
Output is correct |
27 |
Correct |
153 ms |
520964 KB |
Output is correct |
28 |
Correct |
228 ms |
526600 KB |
Output is correct |
29 |
Correct |
163 ms |
513092 KB |
Output is correct |