#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define for1(i,j,k) for(int i=(j);i<=(k);i++)
#define for2(i,j,k) for(int i=(j);i>=(k);i--)
#define for3(i,j,k,l) for(int i=j;i<=k;i+=l)
#define bit(n,i) ((n>>i)&1)
#define all(x) x.begin(),x.end()
#pragma GCC optimize("O2,unroll-loops")
#pragma GCC target("avx,avx2,bmi,bmi2,sse,sse2,sse3,ssse3,sse4,popcnt")
#define int long long
typedef long long ll;
typedef pair<int,int> pii;
typedef long double ld;
typedef pair<ld,ld> pdd;
typedef pair<ll,ll> pll;
const ll maxn=2e6+6;
const ll offset=1e9;
const ll block_sz=317;
const ll inf=1e18;
const ll mod=123456789;
int n,m;
int node[maxn][4],Time;
vector<int> L[maxn];
string s[maxn];
void Transform(string& s)
{
for(char& c:s)
{
if (c=='U') c='B';
else if (c=='G') c='D';
}
}
void sol()
{
cin >> n>> m;
for1(i,1,n)
{
cin >> s[i];
Transform(s[i]);
}
memset(node,-1,sizeof(node));
sort(s+1,s+1+n);
for1(i,1,n)
{
int cur=0;
string x=s[i];
reverse(all(x));
for(char c:x)
{
if (node[cur][c-'A']==-1) node[cur][c-'A']=++Time;
cur=node[cur][c-'A'];
L[cur].pb(i);
}
}
for1(i,1,m)
{
string p,q;
cin>>p>>q;
Transform(p);
Transform(q);
int l=lower_bound(s+1,s+1+n,p)-s;
p.back()++;
int r=lower_bound(s+1,s+1+n,p)-s-1;
if (r<l)
{
cout << "0\n";
continue;
}
reverse(all(q));
int cur=0;
for(char c:q)
{
cur=node[cur][c-'A'];
if (cur==-1) break;
}
if (cur==-1)
{
cout << "0\n";
continue;
}
cout << upper_bound(all(L[cur]),r)-lower_bound(all(L[cur]),l)<<'\n';
}
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// freopen("PAINT.inp","r",stdin);
// freopen("PAINT.out","w",stdout);
int t=1;//cin>>t;
while (t--)
{
sol();
}
}
/*
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
76 ms |
172496 KB |
Output is correct |
2 |
Correct |
95 ms |
172624 KB |
Output is correct |
3 |
Correct |
69 ms |
172628 KB |
Output is correct |
4 |
Correct |
75 ms |
172628 KB |
Output is correct |
5 |
Correct |
79 ms |
172600 KB |
Output is correct |
6 |
Correct |
71 ms |
172468 KB |
Output is correct |
7 |
Correct |
84 ms |
172628 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
172 ms |
239432 KB |
Output is correct |
2 |
Correct |
179 ms |
237192 KB |
Output is correct |
3 |
Correct |
123 ms |
199956 KB |
Output is correct |
4 |
Correct |
114 ms |
199472 KB |
Output is correct |
5 |
Correct |
139 ms |
214748 KB |
Output is correct |
6 |
Correct |
139 ms |
215376 KB |
Output is correct |
7 |
Correct |
106 ms |
195408 KB |
Output is correct |
8 |
Correct |
155 ms |
215120 KB |
Output is correct |
9 |
Correct |
144 ms |
211796 KB |
Output is correct |
10 |
Correct |
128 ms |
208788 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
88 ms |
174156 KB |
Output is correct |
2 |
Correct |
85 ms |
174160 KB |
Output is correct |
3 |
Correct |
90 ms |
174164 KB |
Output is correct |
4 |
Correct |
88 ms |
174088 KB |
Output is correct |
5 |
Correct |
85 ms |
174100 KB |
Output is correct |
6 |
Correct |
97 ms |
174028 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
76 ms |
172496 KB |
Output is correct |
2 |
Correct |
95 ms |
172624 KB |
Output is correct |
3 |
Correct |
69 ms |
172628 KB |
Output is correct |
4 |
Correct |
75 ms |
172628 KB |
Output is correct |
5 |
Correct |
79 ms |
172600 KB |
Output is correct |
6 |
Correct |
71 ms |
172468 KB |
Output is correct |
7 |
Correct |
84 ms |
172628 KB |
Output is correct |
8 |
Correct |
172 ms |
239432 KB |
Output is correct |
9 |
Correct |
179 ms |
237192 KB |
Output is correct |
10 |
Correct |
123 ms |
199956 KB |
Output is correct |
11 |
Correct |
114 ms |
199472 KB |
Output is correct |
12 |
Correct |
139 ms |
214748 KB |
Output is correct |
13 |
Correct |
139 ms |
215376 KB |
Output is correct |
14 |
Correct |
106 ms |
195408 KB |
Output is correct |
15 |
Correct |
155 ms |
215120 KB |
Output is correct |
16 |
Correct |
144 ms |
211796 KB |
Output is correct |
17 |
Correct |
128 ms |
208788 KB |
Output is correct |
18 |
Correct |
88 ms |
174156 KB |
Output is correct |
19 |
Correct |
85 ms |
174160 KB |
Output is correct |
20 |
Correct |
90 ms |
174164 KB |
Output is correct |
21 |
Correct |
88 ms |
174088 KB |
Output is correct |
22 |
Correct |
85 ms |
174100 KB |
Output is correct |
23 |
Correct |
97 ms |
174028 KB |
Output is correct |
24 |
Correct |
184 ms |
230740 KB |
Output is correct |
25 |
Correct |
209 ms |
230888 KB |
Output is correct |
26 |
Correct |
177 ms |
230120 KB |
Output is correct |
27 |
Correct |
152 ms |
198880 KB |
Output is correct |
28 |
Correct |
222 ms |
204812 KB |
Output is correct |
29 |
Correct |
141 ms |
185660 KB |
Output is correct |