#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int jaki[30];
struct wi{
vector<int> Q;
int dokad[4];
}*w;
int nowy=2;
string t[100010];
void dodaj(int co,int gdzie_wco,int gdzie)
{
w[gdzie].Q.push_back(co);
if(gdzie_wco<0)
return;
if(w[gdzie].dokad[jaki[t[co][gdzie_wco]-'A']]==0)
{
w[gdzie].dokad[jaki[t[co][gdzie_wco]-'A']]=nowy;
nowy++;
dodaj(co,gdzie_wco-1,nowy-1);
}
else dodaj(co,gdzie_wco-1,w[gdzie].dokad[jaki[t[co][gdzie_wco]-'A']]);
}
int szukx,szuky;
string suf;
int wynik=0;
void znajdz(int gdzie_wco,int gdzie)
{
if(gdzie_wco<0)
{
int pocz=0,kon=w[gdzie].Q.size(),sre,ostd=-1;
while(pocz<kon)
{
sre=(pocz+kon)/2;
if(w[gdzie].Q[sre]<szukx)
pocz=sre+1;
else{
ostd=sre;
kon=sre;
}
}
int poczz=ostd;
pocz=0;
kon=w[gdzie].Q.size(),sre,ostd=-1;
while(pocz<kon)
{
sre=(pocz+kon)/2;
if(w[gdzie].Q[sre]>szuky)
kon=sre;
else{
ostd=sre;
pocz=sre+1;
}
}
if(poczz==-1 || ostd==-1)
return;
wynik=(ostd-poczz+1);
return;
}
if(w[gdzie].dokad[jaki[suf[gdzie_wco]-'A']]==0)
return;
znajdz(gdzie_wco-1,w[gdzie].dokad[jaki[suf[gdzie_wco]-'A']]);
}
int main()
{
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio(0);
jaki['A'-'A']=0;
jaki['G'-'A']=1;
jaki['C'-'A']=2;
jaki['U'-'A']=3;
w=new wi[2000004];
int n,m;
cin>>n>>m;
string s;
for(int i=0;i<n;i++)
{
cin>>s;
t[i]=s;
}
sort(t,t+n);
for(int i=0;i<n;i++)
dodaj(i,t[i].length()-1,1);
string pref;
for(int i=1;i<=m;i++)
{
cin>>pref>>suf;
int pocz=0,kon=n,sre,ostd=-1;
while(pocz<kon)
{
sre=(pocz+kon)/2;
int k=0;
for(int j=0;j<min(pref.length(),t[sre].length());j++)
{
if(pref[j]<t[sre][j])
{
k=1;
break;
}
if(pref[j]>t[sre][j])
{
k=2;
break;
}
}
if(k==2)
pocz=sre+1;
else if(k==1)
kon=sre;
if(k==0 && t[sre].length()>=pref.length())
{
kon=sre;
ostd=sre;
}
if(k==0 && t[sre].length()<pref.length())
pocz=sre+1;
}
int poczz=ostd;
pocz=0;
kon=n;
ostd=-1;
while(pocz<kon)
{
sre=(pocz+kon)/2;
int k=0;
for(int j=0;j<min(pref.length(),t[sre].length());j++)
{
if(pref[j]<t[sre][j])
{
k=1;
break;
}
if(pref[j]>t[sre][j])
{
k=2;
break;
}
}
if(k==1)
kon=sre;
if(k==2)
pocz=sre+1;
if(k==0 && pref.length()<=t[sre].length())
{
ostd=sre;
pocz=sre+1;
}
if(k==0 && pref.length()>t[sre].length())
pocz=sre+1;
}
if(poczz==-1 || ostd==-1)
{
cout<<"0\n";
continue;
}
szukx=poczz;
szuky=ostd;
wynik=0;
znajdz(suf.length()-1,1);
cout<<wynik<<"\n";
}
return 0;
}
Compilation message
selling_rna.cpp: In function 'void znajdz(int, int)':
selling_rna.cpp:45:41: warning: right operand of comma operator has no effect [-Wunused-value]
45 | kon=w[gdzie].Q.size(),sre,ostd=-1;
| ^
selling_rna.cpp: In function 'int main()':
selling_rna.cpp:95:26: warning: comparison of integer expressions of different signedness: 'int' and 'const long unsigned int' [-Wsign-compare]
95 | for(int j=0;j<min(pref.length(),t[sre].length());j++)
| ~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
selling_rna.cpp:128:26: warning: comparison of integer expressions of different signedness: 'int' and 'const long unsigned int' [-Wsign-compare]
128 | for(int j=0;j<min(pref.length(),t[sre].length());j++)
| ~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
81636 KB |
Output is correct |
2 |
Correct |
32 ms |
81728 KB |
Output is correct |
3 |
Correct |
32 ms |
81712 KB |
Output is correct |
4 |
Correct |
33 ms |
81680 KB |
Output is correct |
5 |
Correct |
31 ms |
81660 KB |
Output is correct |
6 |
Correct |
32 ms |
81732 KB |
Output is correct |
7 |
Correct |
31 ms |
81724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
156 ms |
149336 KB |
Output is correct |
2 |
Correct |
172 ms |
146112 KB |
Output is correct |
3 |
Correct |
96 ms |
98892 KB |
Output is correct |
4 |
Correct |
93 ms |
99084 KB |
Output is correct |
5 |
Correct |
103 ms |
123688 KB |
Output is correct |
6 |
Correct |
102 ms |
124296 KB |
Output is correct |
7 |
Correct |
103 ms |
97100 KB |
Output is correct |
8 |
Correct |
166 ms |
117896 KB |
Output is correct |
9 |
Correct |
150 ms |
113932 KB |
Output is correct |
10 |
Correct |
120 ms |
110956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
64 ms |
83284 KB |
Output is correct |
2 |
Correct |
56 ms |
82984 KB |
Output is correct |
3 |
Correct |
61 ms |
82984 KB |
Output is correct |
4 |
Correct |
55 ms |
82964 KB |
Output is correct |
5 |
Correct |
62 ms |
82940 KB |
Output is correct |
6 |
Correct |
66 ms |
82996 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
81636 KB |
Output is correct |
2 |
Correct |
32 ms |
81728 KB |
Output is correct |
3 |
Correct |
32 ms |
81712 KB |
Output is correct |
4 |
Correct |
33 ms |
81680 KB |
Output is correct |
5 |
Correct |
31 ms |
81660 KB |
Output is correct |
6 |
Correct |
32 ms |
81732 KB |
Output is correct |
7 |
Correct |
31 ms |
81724 KB |
Output is correct |
8 |
Correct |
156 ms |
149336 KB |
Output is correct |
9 |
Correct |
172 ms |
146112 KB |
Output is correct |
10 |
Correct |
96 ms |
98892 KB |
Output is correct |
11 |
Correct |
93 ms |
99084 KB |
Output is correct |
12 |
Correct |
103 ms |
123688 KB |
Output is correct |
13 |
Correct |
102 ms |
124296 KB |
Output is correct |
14 |
Correct |
103 ms |
97100 KB |
Output is correct |
15 |
Correct |
166 ms |
117896 KB |
Output is correct |
16 |
Correct |
150 ms |
113932 KB |
Output is correct |
17 |
Correct |
120 ms |
110956 KB |
Output is correct |
18 |
Correct |
64 ms |
83284 KB |
Output is correct |
19 |
Correct |
56 ms |
82984 KB |
Output is correct |
20 |
Correct |
61 ms |
82984 KB |
Output is correct |
21 |
Correct |
55 ms |
82964 KB |
Output is correct |
22 |
Correct |
62 ms |
82940 KB |
Output is correct |
23 |
Correct |
66 ms |
82996 KB |
Output is correct |
24 |
Correct |
173 ms |
138348 KB |
Output is correct |
25 |
Correct |
192 ms |
138632 KB |
Output is correct |
26 |
Correct |
167 ms |
137804 KB |
Output is correct |
27 |
Correct |
105 ms |
98252 KB |
Output is correct |
28 |
Correct |
274 ms |
104372 KB |
Output is correct |
29 |
Correct |
157 ms |
90588 KB |
Output is correct |