#include <bits/stdc++.h>
using namespace std;
int index(char c){
if(c == 'A') return 0;
if(c == 'G') return 1;
if(c == 'C') return 2;
return 3;
}
struct trie{
vector<vector<int>> t;
vector<vector<int>> inds;
int uj_pont(){
t.push_back(vector<int>(4, -1));
inds.push_back(vector<int>());
return (int)t.size()-1;
}
void init(){
uj_pont();
}
int add(string s, int ind){
int x = 0;
for(char &c : s){
int y = index(c);
if(t[x][y] == -1)
t[x][y] = uj_pont();
x = t[x][y];
}
inds[x].push_back(ind);
return x;
}
};
vector<vector<int>> query;
vector<int> ert, veg, ki;
vector<string> vs;
trie t1, t2;
void add(trie &t, string s){
int x = 0;
for(char &c : s){
ert[x]++;
int y = index(c);
if(t.t[x][y] == -1)
return;
x = t.t[x][y];
}
ert[x]++;
return;
}
void add_querry(trie &t, string s, int ind){
int x = 0;
for(char &c : s){
int y = index(c);
if(t.t[x][y] == -1)
return;
x = t.t[x][y];
}
query[x].push_back(ind);
return;
}
void bejar(trie &t, int x){
for(int &y : query[x])
ki[y] = -ert[veg[y]];
for(int y : t.inds[x])
add(t2, vs[y]);
for(int i = 0; i < 4; i++)
if(t.t[x][i] != -1)
bejar(t, t.t[x][i]);
for(int &y : query[x])
ki[y] += ert[veg[y]];
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin>>n>>m;
t1.init();
t2.init();
vs.resize(n);
for(int i = 0; i < n; i++){
cin>>vs[i];
t1.add(vs[i], i);
reverse(vs[i].begin(), vs[i].end());
}
query.resize(t1.t.size());
ki.resize(m);
veg.resize(m);
for(int i = 0; i < m; i++){
string a, b;
cin>>a>>b;
add_querry(t1, a, i);
reverse(b.begin(), b.end());
veg[i] = t2.add(b, i);
}
ert.resize(t2.t.size(), 0);
bejar(t1, 0);
for(int x : ki)
cout<<x<<"\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
7148 KB |
Output is correct |
2 |
Correct |
37 ms |
7044 KB |
Output is correct |
3 |
Correct |
383 ms |
203340 KB |
Output is correct |
4 |
Correct |
368 ms |
193640 KB |
Output is correct |
5 |
Correct |
327 ms |
174152 KB |
Output is correct |
6 |
Correct |
324 ms |
175832 KB |
Output is correct |
7 |
Correct |
57 ms |
8052 KB |
Output is correct |
8 |
Correct |
255 ms |
128608 KB |
Output is correct |
9 |
Correct |
231 ms |
114120 KB |
Output is correct |
10 |
Correct |
135 ms |
65876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
3732 KB |
Output is correct |
2 |
Correct |
18 ms |
2636 KB |
Output is correct |
3 |
Correct |
23 ms |
3140 KB |
Output is correct |
4 |
Correct |
19 ms |
2636 KB |
Output is correct |
5 |
Correct |
20 ms |
2892 KB |
Output is correct |
6 |
Correct |
24 ms |
3388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
35 ms |
7148 KB |
Output is correct |
9 |
Correct |
37 ms |
7044 KB |
Output is correct |
10 |
Correct |
383 ms |
203340 KB |
Output is correct |
11 |
Correct |
368 ms |
193640 KB |
Output is correct |
12 |
Correct |
327 ms |
174152 KB |
Output is correct |
13 |
Correct |
324 ms |
175832 KB |
Output is correct |
14 |
Correct |
57 ms |
8052 KB |
Output is correct |
15 |
Correct |
255 ms |
128608 KB |
Output is correct |
16 |
Correct |
231 ms |
114120 KB |
Output is correct |
17 |
Correct |
135 ms |
65876 KB |
Output is correct |
18 |
Correct |
25 ms |
3732 KB |
Output is correct |
19 |
Correct |
18 ms |
2636 KB |
Output is correct |
20 |
Correct |
23 ms |
3140 KB |
Output is correct |
21 |
Correct |
19 ms |
2636 KB |
Output is correct |
22 |
Correct |
20 ms |
2892 KB |
Output is correct |
23 |
Correct |
24 ms |
3388 KB |
Output is correct |
24 |
Correct |
47 ms |
8460 KB |
Output is correct |
25 |
Correct |
61 ms |
9404 KB |
Output is correct |
26 |
Correct |
42 ms |
8120 KB |
Output is correct |
27 |
Correct |
333 ms |
168028 KB |
Output is correct |
28 |
Correct |
104 ms |
14148 KB |
Output is correct |
29 |
Correct |
77 ms |
9400 KB |
Output is correct |