#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O2,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("Os")
#define F first
#define S second
#define pb push_back
#define SZ(x) (ll)(x.size())
#define all(x) x.begin(),x.end()
#define MP make_pair
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pll;
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll maxn=2e6+10, maxm=1e3+10, lg=21, mod=1e9+7, inf=1e18;
ll n,q,C[maxn][4],nn,tt,wat[maxn],st[maxn],ft[maxn],cnt[maxn],ans[maxn];
vector<ll> qq[maxn];
vector<pll> Q[maxn];
string s[maxn],pre[maxn],suf[maxn];
ll add(string s){
ll v=0;
for(auto c:s) cnt[v=C[v][c-'a']=(!C[v][c-'a'] ? ++nn:C[v][c-'a'])]++;
return v;
}
ll dff(string s){
ll v=0;
for(auto c:s){
v=C[v][c-'a'];
if(v==0) break;
}
return v;
}
void dfs(ll v=0){
wat[st[v]=tt++]=v;
for(int i:{0,1,2,3})if(C[v][i]) dfs(C[v][i]);
ft[v]=tt;
}
map<char,char> num;
void mkay(string &s){for(auto &c:s) c=num[c];}
int main(){
ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
num['A']='a', num['C']='b', num['G']='c', num['U']='d';
cin>>n>>q;
for(int i=0;i<n;i++){
cin>>s[i]; mkay(s[i]);
qq[add(s[i])].pb(i);
reverse(all(s[i]));
}
dfs();
for(int i=0;i<q;i++){
cin>>pre[i]>>suf[i]; mkay(pre[i]), mkay(suf[i]);
ll v=dff(pre[i]);
if(v){
Q[st[v]].pb({-1,i});
Q[ft[v]].pb({+1,i});
reverse(all(suf[i]));
}
}
memset(C,0,sizeof(C)), memset(cnt,0,sizeof(cnt)), nn=0;
for(int i=0;i<=tt;i++){
for(auto x:Q[i]){
ll v=dff(suf[x.S]);
if(v) ans[x.S]+=x.F*cnt[v];
}
for(auto x:qq[wat[i]]) add(s[x]);
}
for(int i=0;i<q;i++) cout<<ans[i]<<endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
201 ms |
360440 KB |
Output is correct |
2 |
Correct |
199 ms |
360440 KB |
Output is correct |
3 |
Correct |
195 ms |
360440 KB |
Output is correct |
4 |
Correct |
198 ms |
360516 KB |
Output is correct |
5 |
Correct |
193 ms |
360440 KB |
Output is correct |
6 |
Correct |
201 ms |
360440 KB |
Output is correct |
7 |
Correct |
195 ms |
360440 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
288 ms |
366072 KB |
Output is correct |
2 |
Correct |
296 ms |
369156 KB |
Output is correct |
3 |
Correct |
376 ms |
414456 KB |
Output is correct |
4 |
Correct |
370 ms |
412280 KB |
Output is correct |
5 |
Correct |
344 ms |
394872 KB |
Output is correct |
6 |
Correct |
343 ms |
395128 KB |
Output is correct |
7 |
Correct |
288 ms |
371960 KB |
Output is correct |
8 |
Correct |
342 ms |
389752 KB |
Output is correct |
9 |
Correct |
322 ms |
386808 KB |
Output is correct |
10 |
Correct |
290 ms |
381688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
319 ms |
364136 KB |
Output is correct |
2 |
Correct |
275 ms |
363124 KB |
Output is correct |
3 |
Correct |
291 ms |
363752 KB |
Output is correct |
4 |
Correct |
283 ms |
363032 KB |
Output is correct |
5 |
Correct |
285 ms |
362920 KB |
Output is correct |
6 |
Correct |
318 ms |
363512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
201 ms |
360440 KB |
Output is correct |
2 |
Correct |
199 ms |
360440 KB |
Output is correct |
3 |
Correct |
195 ms |
360440 KB |
Output is correct |
4 |
Correct |
198 ms |
360516 KB |
Output is correct |
5 |
Correct |
193 ms |
360440 KB |
Output is correct |
6 |
Correct |
201 ms |
360440 KB |
Output is correct |
7 |
Correct |
195 ms |
360440 KB |
Output is correct |
8 |
Correct |
288 ms |
366072 KB |
Output is correct |
9 |
Correct |
296 ms |
369156 KB |
Output is correct |
10 |
Correct |
376 ms |
414456 KB |
Output is correct |
11 |
Correct |
370 ms |
412280 KB |
Output is correct |
12 |
Correct |
344 ms |
394872 KB |
Output is correct |
13 |
Correct |
343 ms |
395128 KB |
Output is correct |
14 |
Correct |
288 ms |
371960 KB |
Output is correct |
15 |
Correct |
342 ms |
389752 KB |
Output is correct |
16 |
Correct |
322 ms |
386808 KB |
Output is correct |
17 |
Correct |
290 ms |
381688 KB |
Output is correct |
18 |
Correct |
319 ms |
364136 KB |
Output is correct |
19 |
Correct |
275 ms |
363124 KB |
Output is correct |
20 |
Correct |
291 ms |
363752 KB |
Output is correct |
21 |
Correct |
283 ms |
363032 KB |
Output is correct |
22 |
Correct |
285 ms |
362920 KB |
Output is correct |
23 |
Correct |
318 ms |
363512 KB |
Output is correct |
24 |
Correct |
323 ms |
370424 KB |
Output is correct |
25 |
Correct |
401 ms |
372472 KB |
Output is correct |
26 |
Correct |
307 ms |
369656 KB |
Output is correct |
27 |
Correct |
410 ms |
406992 KB |
Output is correct |
28 |
Correct |
540 ms |
376960 KB |
Output is correct |
29 |
Correct |
472 ms |
370136 KB |
Output is correct |