#include <bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#define pp push_back
#define all(x) (x).begin(),(x).end()
using namespace std;
const char el ='\n';
const char sp = ' ';
const int maxn = 2e6+5;
int n,m;
string s[maxn];
int binsearch(const string& a)
{
int l = 1, r = n, mid;
while(l<=r)
{
mid = (l+r)/2;
int flag = 0;
for(int i=0;i<a.size();++i){
if(i>=s[mid].size() or s[mid][i]!=a[i]){
if(a[i]>s[mid][i]) flag = 1;
else flag = 2;
break;
}
}
if(flag==1) l = mid + 1;
else r = mid - 1;
}
return l;
}
int binsearch1(const string& a)
{
int l = 1, r = n, mid;
while(l<=r)
{
mid = (l+r)/2;
int flag = 0;
for(int i=0;i<a.size();++i){
if(i>=s[mid].size() or s[mid][i]!=a[i]){
if(a[i]>s[mid][i]) flag = 1;
else flag = 2;
break;
}
}
if(flag==1 or flag ==0) l = mid + 1;
else r = mid - 1;
}
return r;
}
struct Trie
{
int trie[maxn][5],curnode = 0;
vector <int> id[maxn];
void add(const string& a, int id_)
{
int node = 0;
for(char c:a)
{
if(!trie[node][c-'a']) trie[node][c-'a'] = ++curnode;
node = trie[node][c-'a'];
id[node].pp(id_);
}
}
int cal(const string& a, int l, int r)
{
int node = 0;
for(char c:a)
{
if(!trie[node][c-'a']) return 0;
node = trie[node][c-'a'];
}
int le = lower_bound(all(id[node]),l) - id[node].begin();
int ri = upper_bound(all(id[node]),r) - id[node].begin();
return ri - le;
}
}f;
char cal(char c)
{
if(c=='A') return 'a';
if(c=='U') return 'b';
if(c=='G') return 'c';
if(c=='C') return 'd';
}
void input()
{
cin>>n>>m;
for(int i=1;i<=n;++i)
{
cin>>s[i];
for(int j=0;j<s[i].size();++j) s[i][j] = cal(s[i][j]);
}
sort(s+1,s+n+1);
for(int i=1;i<=n;++i)
{
string tmp = s[i];
reverse(all(tmp));
f.add(tmp,i);
}
for(int i=0;i<=f.curnode;++i) sort(all(f.id[i]));
}
void solve()
{
string p,q;
for(int i=1;i<=m;++i)
{
cin>>p>>q;
for(int j=0;j<p.size();++j) p[j] = cal(p[j]);
for(int j=0;j<q.size();++j) q[j] = cal(q[j]);
reverse(all(q));
int l = binsearch(p), r = binsearch1(p);
cout<<f.cal(q,l,r)<<el;
}
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
input();
solve();
return 0;
}
Compilation message
selling_rna.cpp: In function 'int binsearch(const string&)':
selling_rna.cpp:20:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
20 | for(int i=0;i<a.size();++i){
| ~^~~~~~~~~
selling_rna.cpp:21:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
21 | if(i>=s[mid].size() or s[mid][i]!=a[i]){
| ~^~~~~~~~~~~~~~~
selling_rna.cpp: In function 'int binsearch1(const string&)':
selling_rna.cpp:40:22: 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<a.size();++i){
| ~^~~~~~~~~
selling_rna.cpp:41:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
41 | if(i>=s[mid].size() or s[mid][i]!=a[i]){
| ~^~~~~~~~~~~~~~~
selling_rna.cpp: In function 'void input()':
selling_rna.cpp:97:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
97 | for(int j=0;j<s[i].size();++j) s[i][j] = cal(s[i][j]);
| ~^~~~~~~~~~~~
selling_rna.cpp: In function 'void solve()':
selling_rna.cpp:115:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
115 | for(int j=0;j<p.size();++j) p[j] = cal(p[j]);
| ~^~~~~~~~~
selling_rna.cpp:116:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
116 | for(int j=0;j<q.size();++j) q[j] = cal(q[j]);
| ~^~~~~~~~~
selling_rna.cpp: In function 'char cal(char)':
selling_rna.cpp:89:1: warning: control reaches end of non-void function [-Wreturn-type]
89 | }
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
111964 KB |
Output is correct |
2 |
Correct |
25 ms |
111964 KB |
Output is correct |
3 |
Correct |
28 ms |
111964 KB |
Output is correct |
4 |
Correct |
30 ms |
111876 KB |
Output is correct |
5 |
Correct |
28 ms |
111956 KB |
Output is correct |
6 |
Correct |
27 ms |
112016 KB |
Output is correct |
7 |
Correct |
25 ms |
111964 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
177 ms |
212816 KB |
Output is correct |
2 |
Correct |
165 ms |
208724 KB |
Output is correct |
3 |
Correct |
75 ms |
125640 KB |
Output is correct |
4 |
Correct |
68 ms |
125232 KB |
Output is correct |
5 |
Correct |
114 ms |
175188 KB |
Output is correct |
6 |
Correct |
115 ms |
176288 KB |
Output is correct |
7 |
Correct |
79 ms |
121940 KB |
Output is correct |
8 |
Correct |
130 ms |
156220 KB |
Output is correct |
9 |
Correct |
117 ms |
150072 KB |
Output is correct |
10 |
Correct |
106 ms |
149004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
46 ms |
112980 KB |
Output is correct |
2 |
Correct |
46 ms |
112820 KB |
Output is correct |
3 |
Correct |
49 ms |
112732 KB |
Output is correct |
4 |
Correct |
44 ms |
112724 KB |
Output is correct |
5 |
Correct |
43 ms |
112732 KB |
Output is correct |
6 |
Correct |
49 ms |
112732 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
111964 KB |
Output is correct |
2 |
Correct |
25 ms |
111964 KB |
Output is correct |
3 |
Correct |
28 ms |
111964 KB |
Output is correct |
4 |
Correct |
30 ms |
111876 KB |
Output is correct |
5 |
Correct |
28 ms |
111956 KB |
Output is correct |
6 |
Correct |
27 ms |
112016 KB |
Output is correct |
7 |
Correct |
25 ms |
111964 KB |
Output is correct |
8 |
Correct |
177 ms |
212816 KB |
Output is correct |
9 |
Correct |
165 ms |
208724 KB |
Output is correct |
10 |
Correct |
75 ms |
125640 KB |
Output is correct |
11 |
Correct |
68 ms |
125232 KB |
Output is correct |
12 |
Correct |
114 ms |
175188 KB |
Output is correct |
13 |
Correct |
115 ms |
176288 KB |
Output is correct |
14 |
Correct |
79 ms |
121940 KB |
Output is correct |
15 |
Correct |
130 ms |
156220 KB |
Output is correct |
16 |
Correct |
117 ms |
150072 KB |
Output is correct |
17 |
Correct |
106 ms |
149004 KB |
Output is correct |
18 |
Correct |
46 ms |
112980 KB |
Output is correct |
19 |
Correct |
46 ms |
112820 KB |
Output is correct |
20 |
Correct |
49 ms |
112732 KB |
Output is correct |
21 |
Correct |
44 ms |
112724 KB |
Output is correct |
22 |
Correct |
43 ms |
112732 KB |
Output is correct |
23 |
Correct |
49 ms |
112732 KB |
Output is correct |
24 |
Correct |
180 ms |
195920 KB |
Output is correct |
25 |
Correct |
199 ms |
195924 KB |
Output is correct |
26 |
Correct |
166 ms |
194896 KB |
Output is correct |
27 |
Correct |
80 ms |
124508 KB |
Output is correct |
28 |
Correct |
182 ms |
133716 KB |
Output is correct |
29 |
Correct |
97 ms |
117316 KB |
Output is correct |