#include <bits/stdc++.h>
using namespace std;
const int N = 2e6 + 5;
int st[N], fn[N], timer, l, r;
string s[N/20 + 1], p, q;
void convert(string &s) {
for (int i = 0; i < (int)s.size(); i++) {
if (s[i] == 'A') s[i] = 0;
if (s[i] == 'G') s[i] = 1;
if (s[i] == 'C') s[i] = 2;
if (s[i] == 'U') s[i] = 3;
}
return ;
}
struct Trie {
vector <int*> vec = {new int[5]{}};
inline int* operator [] (int i) {return vec[i];}
inline void add(string s) {
int cur = 0;
for (int i = 0; i < (int)s.size(); i++) {
if (!vec[cur][(int)s[i]]) vec[cur][(int)s[i]] = vec.size(), vec.push_back(new int[5]{});;
cur = vec[cur][(int)s[i]];
vec[cur][4]++;
}
return ;
}
inline int get(string s) {
int cur = 0;
for (int i = 0; i < (int)s.size(); i++) {
if (!vec[cur][(int)s[i]]) return 0;
cur = vec[cur][(int)s[i]];
}
return cur;
}
inline int count(string s) {return vec[get(s)][4];}
} T, seg[N<<2];
void dfs(int v) {
st[v] = timer++;
for (int i = 0; i < 4; i++) if (T[v][i]) dfs(T[v][i]);
fn[v] = timer;
}
void Add(int L = 0, int R = timer, int id = 1) {
seg[id].add(q);
if (R - L == 1) return ;
int mid = (L+R)>>1;
if (l < mid) Add(L, mid, id<<1);
else Add(mid, R, id<<1|1);
return ;
}
int Get(int L = 0, int R = timer, int id = 1) {
if (r <= L || R <= l) return 0;
if (l <= L && R <= r) return seg[id].count(q);
int mid = (L+R)>>1;
return Get(L, mid, id<<1) + Get(mid, R, id<<1|1);
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n, m; cin >> n >> m;
for (int i = 0; i < n; i++) cin >> s[i], convert(s[i]), T.add(s[i]);
dfs(0);
for (int i = 0; i < n; i++) {
int v = T.get(s[i]);
reverse(s[i].begin(), s[i].end());
q = s[i], l = st[v];
Add();
}
for (int i = 0; i < m; i++){
cin >> p >> q, convert(p), convert(q);
int v = T.get(p);
if (v == 0) {cout << 0 << "\n"; continue ;}
reverse(q.begin(), q.end());
l = st[v], r = fn[v];
cout << Get() << "\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
731 ms |
692348 KB |
Output is correct |
2 |
Correct |
727 ms |
692344 KB |
Output is correct |
3 |
Correct |
730 ms |
692344 KB |
Output is correct |
4 |
Correct |
711 ms |
692472 KB |
Output is correct |
5 |
Correct |
723 ms |
692600 KB |
Output is correct |
6 |
Correct |
786 ms |
692476 KB |
Output is correct |
7 |
Correct |
747 ms |
692472 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1071 ms |
1048580 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
803 ms |
692636 KB |
Output is correct |
2 |
Correct |
851 ms |
695032 KB |
Output is correct |
3 |
Correct |
809 ms |
693936 KB |
Output is correct |
4 |
Correct |
782 ms |
692556 KB |
Output is correct |
5 |
Correct |
784 ms |
694648 KB |
Output is correct |
6 |
Correct |
786 ms |
693924 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
731 ms |
692348 KB |
Output is correct |
2 |
Correct |
727 ms |
692344 KB |
Output is correct |
3 |
Correct |
730 ms |
692344 KB |
Output is correct |
4 |
Correct |
711 ms |
692472 KB |
Output is correct |
5 |
Correct |
723 ms |
692600 KB |
Output is correct |
6 |
Correct |
786 ms |
692476 KB |
Output is correct |
7 |
Correct |
747 ms |
692472 KB |
Output is correct |
8 |
Runtime error |
1071 ms |
1048580 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
9 |
Halted |
0 ms |
0 KB |
- |