#include <bits/stdc++.h>
#define ll long long
using namespace std;
int n,m;
const int mx=1e5+5;
const int mxx=2e6+5;
string s[mx],s1,s2;
struct dl{
set <int> s;
int child[5];
};
dl t1[mxx],t2[mxx];
void updn(int j){
t1[j].s={};
for (int i=0;i<5;i++) t1[j].child[i]=-1;
}
void updn1(int j){
t2[j].s={};
for (int i=0;i<5;i++) t2[j].child[i]=-1;
}
int p(char s){
if (s=='A') return 1;
if (s=='U') return 2;
if (s=='G') return 3;
if (s=='C') return 4;
}
void ndl(){
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
}
int cnt=0,cnt1=0;
void add1(string s,int q){
int u=0,n=s.length();
s=" "+s;
for (int i=1;i<=n;i++){
int k=p(s[i]);
if (t1[u].child[k]==-1){
updn(++cnt);
t1[u].child[k]=cnt;
}
u=t1[u].child[k];
t1[u].s.insert(q);
}
}
void add2(string s, int q){
int u=0,n=s.length();
s=" "+s;
for (int i=n;i>=1;i--){
int k=p(s[i]);
if (t2[u].child[k]==-1){
updn1(++cnt1);
t2[u].child[k]=cnt1;
}
u=t2[u].child[k];
t2[u].s.insert(q);
}
}
set <int> se1,se2;
int main(){
ndl();
cin >> n >>m;
updn(0);
updn1(0);
for (int i=1;i<=n;i++){
cin >> s[i];
add1(s[i],i);
add2(s[i],i);
}
set <int> :: iterator it,it1;
while (m--){
se1={};
se2={};
int kt=0;
int res=0;
cin >> s1 >> s2;
int u=0;
for (int i=0;i<s1.length();i++){
int k=p(s1[i]);
if (t1[u].child[k]==-1){
cout << 0<<'\n';
kt=1;
break;
}
u=t1[u].child[k];
}
if (kt) continue;
for (it=t1[u].s.begin();it!=t1[u].s.end();it++){
se1.insert(*it);
}
u=0;
for (int i=s2.length()-1;i>=0;i--){
int k=p(s2[i]);
if (t2[u].child[k]==-1){
cout <<0<<'\n';
kt=1;
break;
}
u=t2[u].child[k];
}
if (kt) continue;
for (it1=t2[u].s.begin();it1!=t2[u].s.end();it1++){
se2.insert(*it1);
}
it=se1.begin();
it1=se2.begin();
while (it!=se1.end() && it1!=se2.end()){
if (*it==*it1){
it++;
it1++;
res++;
continue;
}
if (*it<*it1){
it++;
continue;
}
if (*it>*it1){
it1++;
continue;
}
}
cout << res<<'\n';
}
}
Compilation message
selling_rna.cpp: In function 'int main()':
selling_rna.cpp:77:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
77 | for (int i=0;i<s1.length();i++){
| ~^~~~~~~~~~~~
selling_rna.cpp: In function 'int p(char)':
selling_rna.cpp:26:1: warning: control reaches end of non-void function [-Wreturn-type]
26 | }
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
53 ms |
285276 KB |
Output is correct |
2 |
Correct |
57 ms |
285224 KB |
Output is correct |
3 |
Correct |
59 ms |
285264 KB |
Output is correct |
4 |
Correct |
49 ms |
285276 KB |
Output is correct |
5 |
Correct |
49 ms |
285268 KB |
Output is correct |
6 |
Correct |
49 ms |
285392 KB |
Output is correct |
7 |
Correct |
57 ms |
285268 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
933 ms |
479228 KB |
Output is correct |
2 |
Execution timed out |
1566 ms |
478728 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1527 ms |
297420 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
53 ms |
285276 KB |
Output is correct |
2 |
Correct |
57 ms |
285224 KB |
Output is correct |
3 |
Correct |
59 ms |
285264 KB |
Output is correct |
4 |
Correct |
49 ms |
285276 KB |
Output is correct |
5 |
Correct |
49 ms |
285268 KB |
Output is correct |
6 |
Correct |
49 ms |
285392 KB |
Output is correct |
7 |
Correct |
57 ms |
285268 KB |
Output is correct |
8 |
Correct |
933 ms |
479228 KB |
Output is correct |
9 |
Execution timed out |
1566 ms |
478728 KB |
Time limit exceeded |
10 |
Halted |
0 ms |
0 KB |
- |