#include <bits/stdc++.h>
#include <unordered_map>
using namespace std;
const int N=1e5+5;
int n,m,ans,l,r;
string s[N],S;
pair<pair<string,string>,int> p[N];
struct trie
{
vector<int> v;
trie *a[26]={0};
void insert(int pos,int idx)
{
v.push_back(idx);
if (pos<0)
return;
int x= S[pos]-'A';
if (!a[x])
a[x]=new trie();
a[x]->insert(pos-1,idx);
}
int query (int pos)
{
if (pos==S.size())
{
if (v.empty()||v.back()!=n)
v.push_back(n);
int i= lower_bound(v.begin(),v.end(),l)-v.begin();
int j= lower_bound(v.begin(),v.end(),r)-v.begin();
return j-i;
}
int x= (S[pos]-'A');
if (!a[x])
return 0;
return a[x]->query(pos+1);
}
}tree;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >>n>>m;
for (int i=0;i<n;i++)
cin >>s[i];
for (int i=0;i<m;i++)
cin >>p[i].first.first>>p[i].first.second,p[i].second=i;
sort(s,s+n);
for (int i=0;i<n;i++)
{
S=s[i];
tree.insert(S.size()-1,i);
}
int idx1=0,idx2=1;
for (int i=0;i<m;i++)
{
string s1=p[i].first.first;
string s2=p[i].first.second;
int low,high,mid;
low=0,high=n-1;
l=r=n;
while (low<=high)
{
mid=(low+high)/2;
if (s[mid].substr(0,min(s[mid].size(),s1.size()))>=s1)
{
l=mid;
high=mid-1;
}
else
low=mid+1;
}
low=0,high=n-1;
while (low<=high)
{
mid=(low+high)/2;
if (s[mid].substr(0,min(s[mid].size(),s1.size()))>s1)
high=mid-1;
else
{
r=mid;
low=mid+1;
}
}
r++;
reverse(s2.begin(),s2.end());
S=s2;
cout <<tree.query(0)<<"\n";
}
return 0;
}
/*
2 3
AUGC
AGC
G C
AU C
A C
3 3
AA
AA
AGA
AA AA
AG GA
AG GA
8 7
GCGCUACCCCAACACAAGGCAAGAUAUA
G
GGAC
GCGG
U
GCGCUACCCCAACACAAGGCAAGAUGGUC
GCCG
GCGCUGA
GCGCUACCC A
GCGCUACCCC AC
GCG C
GCGC A
G G
G C
G GGA
*/
Compilation message
selling_rna.cpp: In member function 'int trie::query(int)':
selling_rna.cpp:24:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
24 | if (pos==S.size())
| ~~~^~~~~~~~~~
selling_rna.cpp: In function 'int main()':
selling_rna.cpp:54:9: warning: unused variable 'idx1' [-Wunused-variable]
54 | int idx1=0,idx2=1;
| ^~~~
selling_rna.cpp:54:16: warning: unused variable 'idx2' [-Wunused-variable]
54 | int idx1=0,idx2=1;
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
10496 KB |
Output is correct |
2 |
Correct |
7 ms |
10496 KB |
Output is correct |
3 |
Correct |
7 ms |
10496 KB |
Output is correct |
4 |
Correct |
7 ms |
10624 KB |
Output is correct |
5 |
Correct |
7 ms |
10496 KB |
Output is correct |
6 |
Correct |
7 ms |
10496 KB |
Output is correct |
7 |
Correct |
7 ms |
10496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
442 ms |
541304 KB |
Output is correct |
2 |
Correct |
445 ms |
513144 KB |
Output is correct |
3 |
Correct |
84 ms |
31864 KB |
Output is correct |
4 |
Correct |
84 ms |
31096 KB |
Output is correct |
5 |
Correct |
358 ms |
339448 KB |
Output is correct |
6 |
Correct |
360 ms |
344312 KB |
Output is correct |
7 |
Correct |
87 ms |
30584 KB |
Output is correct |
8 |
Correct |
320 ms |
217996 KB |
Output is correct |
9 |
Correct |
260 ms |
188664 KB |
Output is correct |
10 |
Correct |
207 ms |
176248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
67 ms |
12276 KB |
Output is correct |
2 |
Correct |
47 ms |
12792 KB |
Output is correct |
3 |
Correct |
58 ms |
12404 KB |
Output is correct |
4 |
Correct |
48 ms |
11768 KB |
Output is correct |
5 |
Correct |
52 ms |
11896 KB |
Output is correct |
6 |
Correct |
63 ms |
11972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
10496 KB |
Output is correct |
2 |
Correct |
7 ms |
10496 KB |
Output is correct |
3 |
Correct |
7 ms |
10496 KB |
Output is correct |
4 |
Correct |
7 ms |
10624 KB |
Output is correct |
5 |
Correct |
7 ms |
10496 KB |
Output is correct |
6 |
Correct |
7 ms |
10496 KB |
Output is correct |
7 |
Correct |
7 ms |
10496 KB |
Output is correct |
8 |
Correct |
442 ms |
541304 KB |
Output is correct |
9 |
Correct |
445 ms |
513144 KB |
Output is correct |
10 |
Correct |
84 ms |
31864 KB |
Output is correct |
11 |
Correct |
84 ms |
31096 KB |
Output is correct |
12 |
Correct |
358 ms |
339448 KB |
Output is correct |
13 |
Correct |
360 ms |
344312 KB |
Output is correct |
14 |
Correct |
87 ms |
30584 KB |
Output is correct |
15 |
Correct |
320 ms |
217996 KB |
Output is correct |
16 |
Correct |
260 ms |
188664 KB |
Output is correct |
17 |
Correct |
207 ms |
176248 KB |
Output is correct |
18 |
Correct |
67 ms |
12276 KB |
Output is correct |
19 |
Correct |
47 ms |
12792 KB |
Output is correct |
20 |
Correct |
58 ms |
12404 KB |
Output is correct |
21 |
Correct |
48 ms |
11768 KB |
Output is correct |
22 |
Correct |
52 ms |
11896 KB |
Output is correct |
23 |
Correct |
63 ms |
11972 KB |
Output is correct |
24 |
Correct |
462 ms |
444884 KB |
Output is correct |
25 |
Correct |
494 ms |
445304 KB |
Output is correct |
26 |
Correct |
419 ms |
439928 KB |
Output is correct |
27 |
Correct |
104 ms |
31224 KB |
Output is correct |
28 |
Correct |
382 ms |
83820 KB |
Output is correct |
29 |
Correct |
180 ms |
19432 KB |
Output is correct |