#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].cnt++;
node[pos].r = i;
}
//node[pos].exist++;
}
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)
{
// cout<<s<<"\n";
int pos = 0;
for(auto f : s)
{
int c = f - 'A';
if(no[pos].chil[c] == -1) no[pos].chil[c] = new_2(i);
// if(c == 'A' - 'A' && pos == 0) cerr<<pos<<" "<<no[pos].chil[c]<<"\n";
pos = no[pos].chil[c];
no[pos].v.pb(i);
// cerr<<pos<<"\n";
}
// cerr<<"\n";
}
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];
}
// cerr<<s<<" "<<l<<" "<<r<<"\n";
// for(auto k : no[pos].v) cerr<<k<<" ";
// cerr<<"\n";
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());
// no[i].v.resize(unique(no[i].v.begin() , no[i].v.end()) - no[i].v.begin());
}
forinc(i,1,m)
{
string s1,s2;
cin>>s1>>s2;
reverse(s2.begin(),s2.end());
int l,r;
fin(s1,l,r);
//cout<<l<<" "<<r<<"\n";
if(l == -1)
{
cout<<0<<"\n";
continue;
}
fin2(s2,l,r);
}
}
Compilation message
selling_rna.cpp: In function 'int32_t main()':
selling_rna.cpp:120:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
120 | freopen(task".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
selling_rna.cpp:121:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
121 | freopen(task".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
124 ms |
471368 KB |
Output is correct |
2 |
Correct |
119 ms |
471460 KB |
Output is correct |
3 |
Correct |
142 ms |
471368 KB |
Output is correct |
4 |
Correct |
171 ms |
473356 KB |
Output is correct |
5 |
Correct |
155 ms |
471528 KB |
Output is correct |
6 |
Correct |
151 ms |
471340 KB |
Output is correct |
7 |
Correct |
148 ms |
471460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
303 ms |
536136 KB |
Output is correct |
2 |
Correct |
279 ms |
532820 KB |
Output is correct |
3 |
Correct |
331 ms |
712008 KB |
Output is correct |
4 |
Correct |
320 ms |
700744 KB |
Output is correct |
5 |
Correct |
315 ms |
653852 KB |
Output is correct |
6 |
Correct |
334 ms |
656460 KB |
Output is correct |
7 |
Correct |
210 ms |
481680 KB |
Output is correct |
8 |
Correct |
334 ms |
585800 KB |
Output is correct |
9 |
Correct |
331 ms |
567368 KB |
Output is correct |
10 |
Correct |
304 ms |
566088 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
190 ms |
472392 KB |
Output is correct |
2 |
Correct |
177 ms |
472208 KB |
Output is correct |
3 |
Correct |
181 ms |
472212 KB |
Output is correct |
4 |
Correct |
163 ms |
472136 KB |
Output is correct |
5 |
Correct |
163 ms |
472540 KB |
Output is correct |
6 |
Correct |
179 ms |
472416 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
124 ms |
471368 KB |
Output is correct |
2 |
Correct |
119 ms |
471460 KB |
Output is correct |
3 |
Correct |
142 ms |
471368 KB |
Output is correct |
4 |
Correct |
171 ms |
473356 KB |
Output is correct |
5 |
Correct |
155 ms |
471528 KB |
Output is correct |
6 |
Correct |
151 ms |
471340 KB |
Output is correct |
7 |
Correct |
148 ms |
471460 KB |
Output is correct |
8 |
Correct |
303 ms |
536136 KB |
Output is correct |
9 |
Correct |
279 ms |
532820 KB |
Output is correct |
10 |
Correct |
331 ms |
712008 KB |
Output is correct |
11 |
Correct |
320 ms |
700744 KB |
Output is correct |
12 |
Correct |
315 ms |
653852 KB |
Output is correct |
13 |
Correct |
334 ms |
656460 KB |
Output is correct |
14 |
Correct |
210 ms |
481680 KB |
Output is correct |
15 |
Correct |
334 ms |
585800 KB |
Output is correct |
16 |
Correct |
331 ms |
567368 KB |
Output is correct |
17 |
Correct |
304 ms |
566088 KB |
Output is correct |
18 |
Correct |
190 ms |
472392 KB |
Output is correct |
19 |
Correct |
177 ms |
472208 KB |
Output is correct |
20 |
Correct |
181 ms |
472212 KB |
Output is correct |
21 |
Correct |
163 ms |
472136 KB |
Output is correct |
22 |
Correct |
163 ms |
472540 KB |
Output is correct |
23 |
Correct |
179 ms |
472416 KB |
Output is correct |
24 |
Correct |
318 ms |
525284 KB |
Output is correct |
25 |
Correct |
330 ms |
525180 KB |
Output is correct |
26 |
Correct |
288 ms |
524616 KB |
Output is correct |
27 |
Correct |
351 ms |
669112 KB |
Output is correct |
28 |
Correct |
316 ms |
489800 KB |
Output is correct |
29 |
Correct |
225 ms |
476832 KB |
Output is correct |