#include<bits/stdc++.h>
#define forinc(i,a,b) for(int i=a;i<=b;i++)
#define fordec(i,a,b) for(int i=a;i>=b;i--)
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
using namespace std;
const int N = 3e6+10;
int n,m;
string a[N];
struct trie
{
int child[26];
int exist,cnt;
int l,r;
} node[N];
int cur;
void st()
{
memset(node[0].child , -1 ,sizeof node[0].child);
node[0].cnt = node[0].exist = node[0].l = node[0].r = 0 ;
}
int new_(int i)
{
cur++;
memset(node[cur].child , -1 ,sizeof node[cur].child);
node[cur].cnt = node[cur].exist = 0 ;
node[cur].l = node[cur].r = i;
return cur;
}
void ad(string s,int i)
{
int pos = 0;
for(auto f : s)
{
int c = f - 'A';
if(node[pos].child[c] == -1) node[pos].child[c] = new_(i);
pos = node[pos].child[c];
node[pos].r = i;
}
}
struct tir
{
int chil[26];
vector<int> v;
}no[N];
int cur2;
void st2()
{
memset(no[0].chil , -1 , sizeof no[0].chil);
no[0].v.clear();
}
int new_2(int i)
{
cur2++;
memset(no[cur2].chil , -1 , sizeof no[cur2].chil);
no[cur2].v.clear();
return cur2;
}
void ad2(string s , int i)
{
int pos = 0;
for(auto f : s)
{
int c = f - 'A';
if(no[pos].chil[c] == -1) no[pos].chil[c] = new_2(i);
pos = no[pos].chil[c];
no[pos].v.pb(i);
}
}
void fin(string s , int &l , int &r)
{
int pos = 0;
for(auto f : s)
{
int c = f - 'A';
if(node[pos].child[c] == -1)
{
l = -1;
r = -1;
return;
}
pos = node[pos].child[c];
}
l = node[pos].l ;
r = node[pos].r ;
}
void fin2(string s , int l , int r)
{
int pos = 0;
for(auto f : s)
{
int c = f - 'A';
if(no[pos].chil[c] == -1)
{
cout<<0<<"\n";
return;
}
pos = no[pos].chil[c];
}
int ll = lower_bound(no[pos].v.begin() , no[pos].v.end() , l) - no[pos].v.begin();
int rr = upper_bound(no[pos].v.begin() , no[pos].v.end() , r) - no[pos].v.begin();
cout<<rr - ll<<"\n";
}
int32_t main()
{
#define task "task"
if(fopen(task".inp","r"))
{
freopen(task".inp","r",stdin);
freopen(task".out","w",stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>n>>m;
forinc(i,1,n) cin>>a[i];
sort(a+1,a+n+1);
st();
forinc(i,1,n) ad(a[i],i);
st2();
forinc(i,1,n)
{
string s = a[i];
reverse(s.begin() , s.end());
ad2(s,i);
}
forinc(i,0,cur2 - 1)
{
sort(no[i].v.begin() , no[i].v.end());
}
forinc(i,1,m)
{
string s1,s2;
cin>>s1>>s2;
reverse(s2.begin(),s2.end());
int l,r;
fin(s1,l,r);
if(l == -1)
{
cout<<0<<"\n";
continue;
}
fin2(s2,l,r);
}
}
Compilation message
selling_rna.cpp: In function 'int32_t main()':
selling_rna.cpp:111:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
111 | freopen(task".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
selling_rna.cpp:112:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
112 | freopen(task".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
115 ms |
471368 KB |
Output is correct |
2 |
Correct |
130 ms |
471368 KB |
Output is correct |
3 |
Correct |
116 ms |
471536 KB |
Output is correct |
4 |
Correct |
128 ms |
471368 KB |
Output is correct |
5 |
Correct |
133 ms |
471368 KB |
Output is correct |
6 |
Correct |
130 ms |
471380 KB |
Output is correct |
7 |
Correct |
180 ms |
471408 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
318 ms |
536136 KB |
Output is correct |
2 |
Correct |
294 ms |
532808 KB |
Output is correct |
3 |
Correct |
351 ms |
712008 KB |
Output is correct |
4 |
Correct |
325 ms |
700744 KB |
Output is correct |
5 |
Correct |
341 ms |
653776 KB |
Output is correct |
6 |
Correct |
348 ms |
656456 KB |
Output is correct |
7 |
Correct |
205 ms |
481864 KB |
Output is correct |
8 |
Correct |
329 ms |
585800 KB |
Output is correct |
9 |
Correct |
334 ms |
567392 KB |
Output is correct |
10 |
Correct |
302 ms |
566088 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
182 ms |
472392 KB |
Output is correct |
2 |
Correct |
183 ms |
472296 KB |
Output is correct |
3 |
Correct |
172 ms |
472336 KB |
Output is correct |
4 |
Correct |
179 ms |
472028 KB |
Output is correct |
5 |
Correct |
181 ms |
472524 KB |
Output is correct |
6 |
Correct |
188 ms |
472464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
115 ms |
471368 KB |
Output is correct |
2 |
Correct |
130 ms |
471368 KB |
Output is correct |
3 |
Correct |
116 ms |
471536 KB |
Output is correct |
4 |
Correct |
128 ms |
471368 KB |
Output is correct |
5 |
Correct |
133 ms |
471368 KB |
Output is correct |
6 |
Correct |
130 ms |
471380 KB |
Output is correct |
7 |
Correct |
180 ms |
471408 KB |
Output is correct |
8 |
Correct |
318 ms |
536136 KB |
Output is correct |
9 |
Correct |
294 ms |
532808 KB |
Output is correct |
10 |
Correct |
351 ms |
712008 KB |
Output is correct |
11 |
Correct |
325 ms |
700744 KB |
Output is correct |
12 |
Correct |
341 ms |
653776 KB |
Output is correct |
13 |
Correct |
348 ms |
656456 KB |
Output is correct |
14 |
Correct |
205 ms |
481864 KB |
Output is correct |
15 |
Correct |
329 ms |
585800 KB |
Output is correct |
16 |
Correct |
334 ms |
567392 KB |
Output is correct |
17 |
Correct |
302 ms |
566088 KB |
Output is correct |
18 |
Correct |
182 ms |
472392 KB |
Output is correct |
19 |
Correct |
183 ms |
472296 KB |
Output is correct |
20 |
Correct |
172 ms |
472336 KB |
Output is correct |
21 |
Correct |
179 ms |
472028 KB |
Output is correct |
22 |
Correct |
181 ms |
472524 KB |
Output is correct |
23 |
Correct |
188 ms |
472464 KB |
Output is correct |
24 |
Correct |
290 ms |
525092 KB |
Output is correct |
25 |
Correct |
320 ms |
525384 KB |
Output is correct |
26 |
Correct |
299 ms |
524616 KB |
Output is correct |
27 |
Correct |
325 ms |
669256 KB |
Output is correct |
28 |
Correct |
279 ms |
489844 KB |
Output is correct |
29 |
Correct |
217 ms |
476876 KB |
Output is correct |