#include<bits/stdc++.h>
using namespace std;
#define int long long
#define forinc(a,b,c) for(int a=b, _c=c; a<=_c; ++a)
#define fordec(a,b,c) for(int a=b, _c=c; a>=_c; --a)
#define forv(a,b) for(auto &a:b)
#define ii pair<int,int>
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define all(a) begin(a),end(a)
#define reset(f,x) memset(f,x,sizeof(f))
#define fasty ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define bit(x,i) (x>>(i-1)&1ll)
#define on(x,i) (x|(1ll<<(i-1)))
#define off(x,i) (x&~(1ll<<(i-1)))
typedef long long ll;
typedef unsigned long long ull;
const int N=1e5+5;
int n,m,it=1;
string s[N],pf[N],sf[N];
map<char,int> dd;
int g[N*20][5],ma[N*20],mi[N*20],sz[N*20],ans[N];
vector<int> st[N],en[N];
void add(int i){
int j=1;
forv(v,s[i]){
if(!g[j][dd[v]]) g[j][dd[v]]=++it;
ma[j]=i, mi[j]=(!mi[j])?i:mi[j];
sz[j]++;
j=g[j][dd[v]];
}
ma[j]=i, mi[j]=(!mi[j])?i:mi[j];
sz[j]++;
}
ii que(string &x){
int j=1;
forv(v,x){
if(!g[j][dd[v]]) return make_pair(0,0);
j=g[j][dd[v]];
}
return make_pair(mi[j],ma[j]);
}
int get(string &x){
int j=1;
forv(v,x){
if(!g[j][dd[v]]) return 0;
j=g[j][dd[v]];
}
return sz[j];
}
int32_t main(){
fasty;
dd['A']=1, dd['G']=2, dd['C']=3, dd['U']=4;
cin>>n>>m;
forinc(i,1,n) cin>>s[i];
forinc(i,1,m) cin>>pf[i]>>sf[i];
sort(s+1, s+n+1);
forinc(i,1,n) add(i);
forinc(i,1,m){
ii p=que(pf[i]);
reverse(all(sf[i]));
if(p.fi) st[p.fi-1].pb(i);
en[p.se].pb(i);
}
it=1;
reset(g,0); reset(ma,0); reset(mi,0); reset(sz,0);
forinc(i,1,n){
reverse(all(s[i]));
add(i);
forv(v,st[i]) ans[v]-=get(sf[v]);
forv(v,en[i]) ans[v]+=get(sf[v]);
}
forinc(i,1,m) cout<<ans[i]<<'\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
74 ms |
139640 KB |
Output is correct |
2 |
Correct |
73 ms |
139640 KB |
Output is correct |
3 |
Correct |
75 ms |
139640 KB |
Output is correct |
4 |
Correct |
72 ms |
139640 KB |
Output is correct |
5 |
Correct |
72 ms |
139640 KB |
Output is correct |
6 |
Correct |
73 ms |
139640 KB |
Output is correct |
7 |
Correct |
73 ms |
139640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
206 ms |
147960 KB |
Output is correct |
2 |
Correct |
206 ms |
148092 KB |
Output is correct |
3 |
Correct |
260 ms |
148088 KB |
Output is correct |
4 |
Correct |
256 ms |
148088 KB |
Output is correct |
5 |
Correct |
195 ms |
145148 KB |
Output is correct |
6 |
Correct |
193 ms |
145144 KB |
Output is correct |
7 |
Correct |
156 ms |
151036 KB |
Output is correct |
8 |
Correct |
212 ms |
152056 KB |
Output is correct |
9 |
Correct |
197 ms |
151928 KB |
Output is correct |
10 |
Correct |
151 ms |
146936 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
99 ms |
141936 KB |
Output is correct |
2 |
Correct |
97 ms |
141308 KB |
Output is correct |
3 |
Correct |
97 ms |
141556 KB |
Output is correct |
4 |
Correct |
90 ms |
141176 KB |
Output is correct |
5 |
Correct |
92 ms |
141048 KB |
Output is correct |
6 |
Correct |
98 ms |
141432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
74 ms |
139640 KB |
Output is correct |
2 |
Correct |
73 ms |
139640 KB |
Output is correct |
3 |
Correct |
75 ms |
139640 KB |
Output is correct |
4 |
Correct |
72 ms |
139640 KB |
Output is correct |
5 |
Correct |
72 ms |
139640 KB |
Output is correct |
6 |
Correct |
73 ms |
139640 KB |
Output is correct |
7 |
Correct |
73 ms |
139640 KB |
Output is correct |
8 |
Correct |
206 ms |
147960 KB |
Output is correct |
9 |
Correct |
206 ms |
148092 KB |
Output is correct |
10 |
Correct |
260 ms |
148088 KB |
Output is correct |
11 |
Correct |
256 ms |
148088 KB |
Output is correct |
12 |
Correct |
195 ms |
145148 KB |
Output is correct |
13 |
Correct |
193 ms |
145144 KB |
Output is correct |
14 |
Correct |
156 ms |
151036 KB |
Output is correct |
15 |
Correct |
212 ms |
152056 KB |
Output is correct |
16 |
Correct |
197 ms |
151928 KB |
Output is correct |
17 |
Correct |
151 ms |
146936 KB |
Output is correct |
18 |
Correct |
99 ms |
141936 KB |
Output is correct |
19 |
Correct |
97 ms |
141308 KB |
Output is correct |
20 |
Correct |
97 ms |
141556 KB |
Output is correct |
21 |
Correct |
90 ms |
141176 KB |
Output is correct |
22 |
Correct |
92 ms |
141048 KB |
Output is correct |
23 |
Correct |
98 ms |
141432 KB |
Output is correct |
24 |
Correct |
203 ms |
148856 KB |
Output is correct |
25 |
Correct |
219 ms |
150520 KB |
Output is correct |
26 |
Correct |
205 ms |
148344 KB |
Output is correct |
27 |
Correct |
245 ms |
148728 KB |
Output is correct |
28 |
Correct |
250 ms |
153336 KB |
Output is correct |
29 |
Correct |
180 ms |
146168 KB |
Output is correct |