#include <bits/stdc++.h>
using namespace std;
const int N = 2e6 + 5;
int st[N], fn[N], timer;
string s[N];
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(const int &l, const string &s, int L = 0, int R = timer, int id = 1) {
seg[id].add(s);
if (R - L == 1) return ;
int mid = (L+R)>>1;
if (l < mid) Add(l, s, L, mid, id<<1);
else Add(l, s, mid, R, id<<1|1);
return ;
}
int Get(const int &l, const int &r, const string &s, 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(s);
int mid = (L+R)>>1;
return Get(l, r, s, L, mid, id<<1) + Get(l, r, s, 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());
Add(st[v], s[i]);
}
for (int i = 0; i < m; i++){
string p, q; cin >> p >> q, convert(p), convert(q);
int v = T.get(p);
if (v == 0) {cout << 0 << "\n"; continue ;}
reverse(q.begin(), q.end());
cout << Get(st[v], fn[v], q) << "\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
785 ms |
751908 KB |
Output is correct |
2 |
Correct |
823 ms |
751980 KB |
Output is correct |
3 |
Correct |
740 ms |
751992 KB |
Output is correct |
4 |
Correct |
769 ms |
751936 KB |
Output is correct |
5 |
Correct |
744 ms |
751992 KB |
Output is correct |
6 |
Correct |
794 ms |
752048 KB |
Output is correct |
7 |
Correct |
757 ms |
752056 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1169 ms |
1048576 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 |
770 ms |
752296 KB |
Output is correct |
2 |
Correct |
953 ms |
754740 KB |
Output is correct |
3 |
Correct |
809 ms |
753784 KB |
Output is correct |
4 |
Correct |
786 ms |
752248 KB |
Output is correct |
5 |
Correct |
849 ms |
754156 KB |
Output is correct |
6 |
Correct |
821 ms |
753528 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
785 ms |
751908 KB |
Output is correct |
2 |
Correct |
823 ms |
751980 KB |
Output is correct |
3 |
Correct |
740 ms |
751992 KB |
Output is correct |
4 |
Correct |
769 ms |
751936 KB |
Output is correct |
5 |
Correct |
744 ms |
751992 KB |
Output is correct |
6 |
Correct |
794 ms |
752048 KB |
Output is correct |
7 |
Correct |
757 ms |
752056 KB |
Output is correct |
8 |
Runtime error |
1169 ms |
1048576 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
9 |
Halted |
0 ms |
0 KB |
- |