#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define fi first
#define se second
const int maxN=1e5+5,BLOCK=320;
const ll inf=2e18;
int n,m,ans[maxN+1];
struct query
{
int l,r,id;
}p[maxN+1];
string a[maxN+1],q[maxN+1],b[maxN+1];
struct Trie
{
struct Node
{
Node *child[26];
int cnt,l,r;
Node()
{
for(int i=0;i<26;i++)
{
child[i]=NULL;
}
cnt=l=r=0;
}
};
Node root;
void add(string s,int id)
{
Node *cur=&root;
for(auto c:s)
{
int d=(c-'A');
if(!(cur->child[d]))
{
cur->child[d]=new Node();
}
cur=cur->child[d];
if(!cur->l)
{
cur->l=id;
}
cur->r=id;
cur->cnt++;
}
}
void del(string s)
{
Node *cur=&root;
for(auto c:s)
{
int d=(c-'A');
cur=cur->child[d];
cur->cnt--;
}
}
pii walk(string s)
{
Node *cur=&root;
for(auto c:s)
{
int d=(c-'A');
if(!(cur->child[d]))
{
return {-1,-1};
}
cur=cur->child[d];
}
return {cur->l,cur->r};
}
int Walk(string s)
{
Node *cur=&root;
for(auto c:s)
{
int d=(c-'A');
if(!(cur->child[d]))
{
return 0;
}
cur=cur->child[d];
}
return cur->cnt;
}
}f,f1;
int main()
{
//freopen("","r",stdin);
//freopen("","w",stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>m;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
sort(a+1,a+n+1);
for(int i=1;i<=n;i++)
{
f.add(a[i],i);
//cout<<a[i]<<'\n';
b[i]=a[i];
reverse(b[i].begin(),b[i].end());
}
memset(ans,0,sizeof(ans));
for(int i=1;i<=m;i++)
{
string d;
cin>>d>>q[i];
reverse(q[i].begin(),q[i].end());
pii tmp=f.walk(d);
p[i].l=tmp.fi;
p[i].r=tmp.se;
p[i].id=i;
}
sort(p+1,p+m+1,[&](query x,query y)
{
if(x.l/BLOCK!=y.l/BLOCK)
{
return x.l/BLOCK<y.l/BLOCK;
}
return x.r<y.r;
});
int lo=1,hi=0;
for(int i=1;i<=m;i++)
{
int l=p[i].l,r=p[i].r;
if(l==-1&&r==-1)
{
continue;
}
//cout<<l<<" "<<r<<'\n';
while(hi<r)
{
hi++;
f1.add(b[hi],i);
//cout<<b[hi]<<'\n';
}
while(lo>l)
{
lo--;
f1.add(b[lo],i);
}
while(hi>r)
{
f1.del(b[hi]);
hi--;
}
while(lo<l)
{
f1.del(b[lo]);
lo++;
}
//cout<<q[p[i].id]<<'\n';
ans[p[i].id]=f1.Walk(q[p[i].id]);
}
for(int i=1;i<=m;i++)
{
cout<<ans[i]<<'\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
11096 KB |
Output is correct |
2 |
Correct |
2 ms |
11100 KB |
Output is correct |
3 |
Correct |
2 ms |
11100 KB |
Output is correct |
4 |
Correct |
2 ms |
11100 KB |
Output is correct |
5 |
Correct |
2 ms |
11100 KB |
Output is correct |
6 |
Correct |
2 ms |
11100 KB |
Output is correct |
7 |
Correct |
2 ms |
11100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
467 ms |
481584 KB |
Output is correct |
2 |
Correct |
393 ms |
456528 KB |
Output is correct |
3 |
Correct |
417 ms |
477116 KB |
Output is correct |
4 |
Correct |
343 ms |
454740 KB |
Output is correct |
5 |
Correct |
468 ms |
587824 KB |
Output is correct |
6 |
Correct |
505 ms |
596708 KB |
Output is correct |
7 |
Correct |
203 ms |
21844 KB |
Output is correct |
8 |
Correct |
222 ms |
356944 KB |
Output is correct |
9 |
Correct |
207 ms |
302780 KB |
Output is correct |
10 |
Correct |
153 ms |
291924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
11856 KB |
Output is correct |
2 |
Correct |
14 ms |
12792 KB |
Output is correct |
3 |
Correct |
18 ms |
12324 KB |
Output is correct |
4 |
Correct |
13 ms |
11612 KB |
Output is correct |
5 |
Correct |
16 ms |
12892 KB |
Output is correct |
6 |
Correct |
20 ms |
12368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
11096 KB |
Output is correct |
2 |
Correct |
2 ms |
11100 KB |
Output is correct |
3 |
Correct |
2 ms |
11100 KB |
Output is correct |
4 |
Correct |
2 ms |
11100 KB |
Output is correct |
5 |
Correct |
2 ms |
11100 KB |
Output is correct |
6 |
Correct |
2 ms |
11100 KB |
Output is correct |
7 |
Correct |
2 ms |
11100 KB |
Output is correct |
8 |
Correct |
467 ms |
481584 KB |
Output is correct |
9 |
Correct |
393 ms |
456528 KB |
Output is correct |
10 |
Correct |
417 ms |
477116 KB |
Output is correct |
11 |
Correct |
343 ms |
454740 KB |
Output is correct |
12 |
Correct |
468 ms |
587824 KB |
Output is correct |
13 |
Correct |
505 ms |
596708 KB |
Output is correct |
14 |
Correct |
203 ms |
21844 KB |
Output is correct |
15 |
Correct |
222 ms |
356944 KB |
Output is correct |
16 |
Correct |
207 ms |
302780 KB |
Output is correct |
17 |
Correct |
153 ms |
291924 KB |
Output is correct |
18 |
Correct |
20 ms |
11856 KB |
Output is correct |
19 |
Correct |
14 ms |
12792 KB |
Output is correct |
20 |
Correct |
18 ms |
12324 KB |
Output is correct |
21 |
Correct |
13 ms |
11612 KB |
Output is correct |
22 |
Correct |
16 ms |
12892 KB |
Output is correct |
23 |
Correct |
20 ms |
12368 KB |
Output is correct |
24 |
Correct |
319 ms |
396168 KB |
Output is correct |
25 |
Correct |
316 ms |
396372 KB |
Output is correct |
26 |
Correct |
337 ms |
391760 KB |
Output is correct |
27 |
Correct |
220 ms |
393284 KB |
Output is correct |
28 |
Correct |
105 ms |
69716 KB |
Output is correct |
29 |
Correct |
51 ms |
14780 KB |
Output is correct |