#include <bits/stdc++.h>
using namespace std;
int N, Q;
string s;
int trief[4][2000005];
int trier[4][2000005];
int cntf = 0, cntr = 0;
int lftf[2000005], rhtf[2000005];
int lftr[2000005], rhtr[2000005];
int totf[2000005], totr[2000005];
int pf[200005], pr[200005];
int t = 0;
int qf[200005], qr[200005];
int who[200005];
int bit[4000005];
vector<int> qu[2000005], add[2000005];
int ans[200005];
int getc(char c){
if(c == 'A'){
return 0;
}
else if(c == 'U'){
return 1;
}
else if(c == 'G'){
return 2;
}
else{
return 3;
}
}
void dfs1(int n){
lftf[n] = ++t;
for(int k = 3; k>=0; k--){
if(trief[k][n]){
dfs1(trief[k][n]);
}
}
rhtf[n] = ++t;
}
void dfs2(int n){
lftr[n] = ++t;
for(int k = 3; k>=0; k--){
if(trier[k][n]){
dfs2(trier[k][n]);
}
}
rhtr[n] = ++t;
}
void upd(int n){
while(n <= 2*cntr+2){
bit[n]++;
n += n&-n;
}
}
int query(int n){
int ret = 0;
while(n){
ret += bit[n];
n -= n&-n;
}
return ret;
}
void dfs3(int n){
for(int q : qu[n]){
//cout << "f " << rhtr[qr[q]] << " " << lftr[qr[q]] << "\n";
ans[q] -= query(rhtr[qr[q]]) - query(lftr[qr[q]]-1);
}
for(int k = 3; k>=0; k--){
if(trief[k][n]){
dfs3(trief[k][n]);
}
}
for(int a : add[n]){
//cout << lftr[who[a]] << "\n";
upd(rhtr[who[a]]);
}
for(int q : qu[n]){
//cout << rhtr[qr[q]] << " " << lftr[qr[q]] << "\n";
//cout << query(rhtr[qr[q]]) << " " << query(lftr[qr[q]]) << "\n";
ans[q] += query(rhtr[qr[q]]) - query(lftr[qr[q]]-1);
}
}
int main(){
cin.sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> N >> Q;
for(int i = 1; i<=N; i++){
cin >> s;
int n = 0;
for(char c : s){
int k = getc(c);
if(!trief[k][n]){
trief[k][n] = ++cntf;
}
n = trief[k][n];
}
add[n].emplace_back(i);
totf[n]++;
n = 0;
reverse(s.begin(), s.end());
for(char c : s){
int k = getc(c);
if(!trier[k][n]){
trier[k][n] = ++cntr;
}
n = trier[k][n];
}
totr[n]++;
who[i] = n;
}
//cout << cntf << " " << cntr << "\n";
/*
for(int k : add[1]){
cout << k << " ";
}
cout << "\n";
*/
dfs1(0);
t = 0;
dfs2(0);
for(int i = 1; i<=Q; i++){
cin >> s;
int n = 0;
bool b = 1;
for(char c : s){
int k = getc(c);
if(trief[k][n]){
n = trief[k][n];
}
else{
b = 0;
break;
}
}
qf[i] = n;
n = 0;
cin >> s;
if(!b){
continue;
}
reverse(s.begin(), s.end());
for(char c : s){
int k = getc(c);
if(trier[k][n]){
n = trier[k][n];
}
else{
b = 0;
break;
}
}
qr[i] = n;
if(b){
qu[qf[i]].emplace_back(i);
}
}
dfs3(0);
for(int q = 1; q<=Q; q++){
cout << ans[q] << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
57 ms |
94456 KB |
Output is correct |
2 |
Correct |
57 ms |
94456 KB |
Output is correct |
3 |
Correct |
57 ms |
94456 KB |
Output is correct |
4 |
Correct |
58 ms |
94456 KB |
Output is correct |
5 |
Correct |
57 ms |
94456 KB |
Output is correct |
6 |
Correct |
60 ms |
94456 KB |
Output is correct |
7 |
Correct |
57 ms |
94456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
159 ms |
156536 KB |
Output is correct |
2 |
Correct |
162 ms |
158840 KB |
Output is correct |
3 |
Correct |
198 ms |
148344 KB |
Output is correct |
4 |
Correct |
190 ms |
148728 KB |
Output is correct |
5 |
Correct |
240 ms |
173436 KB |
Output is correct |
6 |
Correct |
245 ms |
174584 KB |
Output is correct |
7 |
Correct |
94 ms |
100216 KB |
Output is correct |
8 |
Correct |
150 ms |
132684 KB |
Output is correct |
9 |
Correct |
147 ms |
133496 KB |
Output is correct |
10 |
Correct |
125 ms |
128888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
79 ms |
95992 KB |
Output is correct |
2 |
Correct |
76 ms |
95608 KB |
Output is correct |
3 |
Correct |
79 ms |
95864 KB |
Output is correct |
4 |
Correct |
71 ms |
95480 KB |
Output is correct |
5 |
Correct |
75 ms |
95480 KB |
Output is correct |
6 |
Correct |
78 ms |
95736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
57 ms |
94456 KB |
Output is correct |
2 |
Correct |
57 ms |
94456 KB |
Output is correct |
3 |
Correct |
57 ms |
94456 KB |
Output is correct |
4 |
Correct |
58 ms |
94456 KB |
Output is correct |
5 |
Correct |
57 ms |
94456 KB |
Output is correct |
6 |
Correct |
60 ms |
94456 KB |
Output is correct |
7 |
Correct |
57 ms |
94456 KB |
Output is correct |
8 |
Correct |
159 ms |
156536 KB |
Output is correct |
9 |
Correct |
162 ms |
158840 KB |
Output is correct |
10 |
Correct |
198 ms |
148344 KB |
Output is correct |
11 |
Correct |
190 ms |
148728 KB |
Output is correct |
12 |
Correct |
240 ms |
173436 KB |
Output is correct |
13 |
Correct |
245 ms |
174584 KB |
Output is correct |
14 |
Correct |
94 ms |
100216 KB |
Output is correct |
15 |
Correct |
150 ms |
132684 KB |
Output is correct |
16 |
Correct |
147 ms |
133496 KB |
Output is correct |
17 |
Correct |
125 ms |
128888 KB |
Output is correct |
18 |
Correct |
79 ms |
95992 KB |
Output is correct |
19 |
Correct |
76 ms |
95608 KB |
Output is correct |
20 |
Correct |
79 ms |
95864 KB |
Output is correct |
21 |
Correct |
71 ms |
95480 KB |
Output is correct |
22 |
Correct |
75 ms |
95480 KB |
Output is correct |
23 |
Correct |
78 ms |
95736 KB |
Output is correct |
24 |
Correct |
163 ms |
155640 KB |
Output is correct |
25 |
Correct |
174 ms |
156536 KB |
Output is correct |
26 |
Correct |
154 ms |
154744 KB |
Output is correct |
27 |
Correct |
181 ms |
142584 KB |
Output is correct |
28 |
Correct |
160 ms |
109560 KB |
Output is correct |
29 |
Correct |
117 ms |
100728 KB |
Output is correct |