#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';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
118 ms |
288332 KB |
Output is correct |
2 |
Correct |
116 ms |
288400 KB |
Output is correct |
3 |
Correct |
116 ms |
288456 KB |
Output is correct |
4 |
Correct |
116 ms |
288428 KB |
Output is correct |
5 |
Correct |
115 ms |
288328 KB |
Output is correct |
6 |
Correct |
120 ms |
288356 KB |
Output is correct |
7 |
Correct |
114 ms |
288472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
243 ms |
347016 KB |
Output is correct |
2 |
Correct |
222 ms |
344216 KB |
Output is correct |
3 |
Correct |
228 ms |
346288 KB |
Output is correct |
4 |
Correct |
226 ms |
343704 KB |
Output is correct |
5 |
Correct |
283 ms |
358260 KB |
Output is correct |
6 |
Correct |
290 ms |
359212 KB |
Output is correct |
7 |
Correct |
159 ms |
291944 KB |
Output is correct |
8 |
Correct |
219 ms |
332056 KB |
Output is correct |
9 |
Correct |
213 ms |
325728 KB |
Output is correct |
10 |
Correct |
189 ms |
324688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
135 ms |
291084 KB |
Output is correct |
2 |
Correct |
135 ms |
290304 KB |
Output is correct |
3 |
Correct |
137 ms |
291048 KB |
Output is correct |
4 |
Correct |
131 ms |
290136 KB |
Output is correct |
5 |
Correct |
133 ms |
290196 KB |
Output is correct |
6 |
Correct |
134 ms |
290532 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
118 ms |
288332 KB |
Output is correct |
2 |
Correct |
116 ms |
288400 KB |
Output is correct |
3 |
Correct |
116 ms |
288456 KB |
Output is correct |
4 |
Correct |
116 ms |
288428 KB |
Output is correct |
5 |
Correct |
115 ms |
288328 KB |
Output is correct |
6 |
Correct |
120 ms |
288356 KB |
Output is correct |
7 |
Correct |
114 ms |
288472 KB |
Output is correct |
8 |
Correct |
243 ms |
347016 KB |
Output is correct |
9 |
Correct |
222 ms |
344216 KB |
Output is correct |
10 |
Correct |
228 ms |
346288 KB |
Output is correct |
11 |
Correct |
226 ms |
343704 KB |
Output is correct |
12 |
Correct |
283 ms |
358260 KB |
Output is correct |
13 |
Correct |
290 ms |
359212 KB |
Output is correct |
14 |
Correct |
159 ms |
291944 KB |
Output is correct |
15 |
Correct |
219 ms |
332056 KB |
Output is correct |
16 |
Correct |
213 ms |
325728 KB |
Output is correct |
17 |
Correct |
189 ms |
324688 KB |
Output is correct |
18 |
Correct |
135 ms |
291084 KB |
Output is correct |
19 |
Correct |
135 ms |
290304 KB |
Output is correct |
20 |
Correct |
137 ms |
291048 KB |
Output is correct |
21 |
Correct |
131 ms |
290136 KB |
Output is correct |
22 |
Correct |
133 ms |
290196 KB |
Output is correct |
23 |
Correct |
134 ms |
290532 KB |
Output is correct |
24 |
Correct |
219 ms |
337936 KB |
Output is correct |
25 |
Correct |
231 ms |
338928 KB |
Output is correct |
26 |
Correct |
222 ms |
336992 KB |
Output is correct |
27 |
Correct |
224 ms |
337216 KB |
Output is correct |
28 |
Correct |
234 ms |
304992 KB |
Output is correct |
29 |
Correct |
177 ms |
293444 KB |
Output is correct |