#include <bits/stdc++.h>
using namespace std;
const int N = 2e6 + 5;
int st[N], fn[N], timer, l, r;
string s[N], 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 |
738 ms |
751868 KB |
Output is correct |
2 |
Correct |
753 ms |
751864 KB |
Output is correct |
3 |
Correct |
807 ms |
751864 KB |
Output is correct |
4 |
Correct |
783 ms |
751892 KB |
Output is correct |
5 |
Correct |
805 ms |
751864 KB |
Output is correct |
6 |
Correct |
781 ms |
751928 KB |
Output is correct |
7 |
Correct |
785 ms |
751928 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1245 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 |
787 ms |
752320 KB |
Output is correct |
2 |
Correct |
891 ms |
754564 KB |
Output is correct |
3 |
Correct |
818 ms |
753528 KB |
Output is correct |
4 |
Correct |
781 ms |
752120 KB |
Output is correct |
5 |
Correct |
814 ms |
754172 KB |
Output is correct |
6 |
Correct |
817 ms |
753400 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
738 ms |
751868 KB |
Output is correct |
2 |
Correct |
753 ms |
751864 KB |
Output is correct |
3 |
Correct |
807 ms |
751864 KB |
Output is correct |
4 |
Correct |
783 ms |
751892 KB |
Output is correct |
5 |
Correct |
805 ms |
751864 KB |
Output is correct |
6 |
Correct |
781 ms |
751928 KB |
Output is correct |
7 |
Correct |
785 ms |
751928 KB |
Output is correct |
8 |
Runtime error |
1245 ms |
1048580 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
9 |
Halted |
0 ms |
0 KB |
- |