#include<bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
using namespace std;
typedef long long ll;
const int maxn=1e5+5;
const int maxm=6e6+5;
map<char,int>m;
struct node{
int a[4];
int cnt=0;
node(){for(int i=0;i<4;i++)a[i]=0;}
};
vector<node>trie[2];
int tin[2][maxm];
int tout[2][maxm];
int vt[2][maxm];
void add(string s,int id,int type){
int pos=0;
for(auto &v:s){
int k=m[v];
if(trie[type][pos].a[k]==0){
trie[type][pos].a[k]=trie[type].size();
trie[type].pb(node());
}
pos=trie[type][pos].a[k];
}
vt[type][id]=pos;
}
int s[2];
void dfs(int id,int type){
tin[type][id]=++s[type];
for(int pos=0;pos<4;pos++){
if(trie[type][id].a[pos]!=0){
dfs(trie[type][id].a[pos],type);
}
}
tout[type][id]=s[type];
}
int n;
string p[2][maxn];
vector<int>Upd[maxm];
vector<int>poi;
int bit[maxn];
void update(int id,int val){
for(int i=id;i<=n;i+=i&(-i)){
bit[i]+=val;
}
}
int get(int id){
int res=0;
for(int i=id;i>=1;i-=i&(-i)){
res+=bit[i];
}
return res;
}
int getrange(int l,int r){
return get(r)-get(l-1);
}
vector<pair<int,int>>P[maxm];
int ans[maxn];
int po2[maxn];
signed main(){
// freopen("input.inp","r",stdin);
// freopen("output.out","w",stdout);
cin.tie(0),cout.tie(0)->sync_with_stdio(0);
cin >> n;
int q;
cin >> q;
trie[0].pb(node());
trie[1].pb(node());
m['A']=0;
m['G']=1;
m['C']=2;
m['U']=3;
for(int i=1;i<=n;i++){
cin >> p[0][i];
p[1][i]=p[0][i];
reverse(p[1][i].begin(),p[1][i].end());
add(p[0][i],i,0);
add(p[1][i],i,1);
}
dfs(0,0);
dfs(0,1);
for(int i=1;i<=n;i++){
poi.pb(tin[1][vt[1][i]]);
Upd[tin[0][vt[0][i]]].pb(tin[1][vt[1][i]]);
}
sort(poi.begin(),poi.end());
poi.resize(unique(poi.begin(),poi.end())-poi.begin());
for(int i=1;i<=q;i++){
string s;
string p;
int pos1=0;
cin >> s >> p;
bool loai=0;
reverse(p.begin(),p.end());
for(int j=0;j<(int)s.size();j++){
if(trie[0][pos1].a[m[s[j]]]==0){
loai=1;
break;
}
else{
pos1=trie[0][pos1].a[m[s[j]]];
}
}
int pos2=0;
for(int j=0;j<(int)p.size();j++){
if(trie[1][pos2].a[m[p[j]]]==0){
loai=1;
break;
}
else{
pos2=trie[1][pos2].a[m[p[j]]];
}
}
if(loai==0){
P[tin[0][pos1]-1].pb({i,-1});
P[tout[0][pos1]].pb({i,1});
po2[i]=pos2;
}
}
for(int i=1;i<=s[0];i++){
for(auto id:Upd[i]){
int st=lower_bound(poi.begin(),poi.end(),id)-poi.begin()+1;
update(st,1);
}
for(auto v:P[i]){
int id=v.fi;
int mul=v.se;
int x=lower_bound(poi.begin(),poi.end(),tin[1][po2[id]])-poi.begin()+1;
int y=upper_bound(poi.begin(),poi.end(),tout[1][po2[id]])-poi.begin();
// cout << x << ' ' << y << '\n';
ans[id]+=getrange(x,y)*mul;
}
}
for(int i=1;i<=q;i++){
cout << ans[i] << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
117 ms |
288332 KB |
Output is correct |
2 |
Correct |
117 ms |
288332 KB |
Output is correct |
3 |
Correct |
115 ms |
288448 KB |
Output is correct |
4 |
Correct |
117 ms |
288388 KB |
Output is correct |
5 |
Correct |
117 ms |
288448 KB |
Output is correct |
6 |
Correct |
121 ms |
288416 KB |
Output is correct |
7 |
Correct |
114 ms |
288336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
234 ms |
346992 KB |
Output is correct |
2 |
Correct |
217 ms |
344100 KB |
Output is correct |
3 |
Correct |
266 ms |
346264 KB |
Output is correct |
4 |
Correct |
220 ms |
343636 KB |
Output is correct |
5 |
Correct |
298 ms |
358184 KB |
Output is correct |
6 |
Correct |
291 ms |
359248 KB |
Output is correct |
7 |
Correct |
160 ms |
291996 KB |
Output is correct |
8 |
Correct |
223 ms |
332132 KB |
Output is correct |
9 |
Correct |
217 ms |
325796 KB |
Output is correct |
10 |
Correct |
191 ms |
324652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
139 ms |
290976 KB |
Output is correct |
2 |
Correct |
154 ms |
290288 KB |
Output is correct |
3 |
Correct |
135 ms |
291020 KB |
Output is correct |
4 |
Correct |
131 ms |
290116 KB |
Output is correct |
5 |
Correct |
132 ms |
290124 KB |
Output is correct |
6 |
Correct |
134 ms |
290504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
117 ms |
288332 KB |
Output is correct |
2 |
Correct |
117 ms |
288332 KB |
Output is correct |
3 |
Correct |
115 ms |
288448 KB |
Output is correct |
4 |
Correct |
117 ms |
288388 KB |
Output is correct |
5 |
Correct |
117 ms |
288448 KB |
Output is correct |
6 |
Correct |
121 ms |
288416 KB |
Output is correct |
7 |
Correct |
114 ms |
288336 KB |
Output is correct |
8 |
Correct |
234 ms |
346992 KB |
Output is correct |
9 |
Correct |
217 ms |
344100 KB |
Output is correct |
10 |
Correct |
266 ms |
346264 KB |
Output is correct |
11 |
Correct |
220 ms |
343636 KB |
Output is correct |
12 |
Correct |
298 ms |
358184 KB |
Output is correct |
13 |
Correct |
291 ms |
359248 KB |
Output is correct |
14 |
Correct |
160 ms |
291996 KB |
Output is correct |
15 |
Correct |
223 ms |
332132 KB |
Output is correct |
16 |
Correct |
217 ms |
325796 KB |
Output is correct |
17 |
Correct |
191 ms |
324652 KB |
Output is correct |
18 |
Correct |
139 ms |
290976 KB |
Output is correct |
19 |
Correct |
154 ms |
290288 KB |
Output is correct |
20 |
Correct |
135 ms |
291020 KB |
Output is correct |
21 |
Correct |
131 ms |
290116 KB |
Output is correct |
22 |
Correct |
132 ms |
290124 KB |
Output is correct |
23 |
Correct |
134 ms |
290504 KB |
Output is correct |
24 |
Correct |
224 ms |
337860 KB |
Output is correct |
25 |
Correct |
232 ms |
339004 KB |
Output is correct |
26 |
Correct |
216 ms |
337008 KB |
Output is correct |
27 |
Correct |
222 ms |
337084 KB |
Output is correct |
28 |
Correct |
230 ms |
304996 KB |
Output is correct |
29 |
Correct |
171 ms |
293444 KB |
Output is correct |