#include <bits/stdc++.h>
using i64 = long long;
constexpr i64 P = 1E9 + 7, M = 5;
struct Node {
Node *to[4];
int mn, mx;
Node() {
mn = 1E9, mx = 0;
to[0] = to[1] = to[2] = to[3] = nullptr;
}
};
void add(Node *u, std::string &s, int i, int p) {
if (i == int(s.size())) {
u->mn = std::min(u->mn, p);
u->mx = std::max(u->mx, p);
return;
}
int x = std::string("AGCU").find(s[i]);
if (u->to[x] == nullptr) {
u->to[x] = new Node();
}
add(u->to[x], s, i + 1, p);
u->mn = std::min(u->mn, p);
u->mx = std::max(u->mx, p);
}
std::pair<int, int> find(Node *u, std::string &s, int i) {
if (i == int(s.size())) {
return {u->mn, u->mx};
}
int x = std::string("AGCU").find(s[i]);
if (u->to[x] == nullptr) {
return {-1, -1};
}
return find(u->to[x], s, i + 1);
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n, m;
std::cin >> n >> m;
std::vector<std::string> s(n);
Node *root = new Node();
for (int i = 0; i < n; i++) {
std::cin >> s[i];
}
std::sort(s.begin(), s.end());
for (int i = 0; i < n; i++) {
add(root, s[i], 0, i);
}
std::vector<std::tuple<int, int, int>> Q;
for (int i = 0; i < m; i++) {
std::string p, q;
std::cin >> p >> q;
auto [l, r] = find(root, p, 0);
if (l == -1) {
continue;
}
r++;
i64 H = 0;
for (char c : q) {
H *= M;
H += std::string("AGCU").find(c) + 1;
H %= P;
}
Q.emplace_back(l, -H, i);
Q.emplace_back(r, H, i);
}
std::sort(Q.begin(), Q.end());
std::vector<int> ans(m);
std::map<int, int> cnt;
int j = 0;
for (int i = 0; i < n; i++) {
int p, H, id;
while (j < int(Q.size())) {
std::tie(p, H, id) = Q[j];
if (p != i) {
break;
}
if (H < 0) {
ans[id] -= cnt[-H];
} else {
ans[id] += cnt[H];
}
j++;
}
i64 h = 0, pw = 1;
for (int j = int(s[i].size()) - 1; j >= 0; j--) {
h = (h + pw * (std::string("AGCU").find(s[i][j]) + 1)) % P;
pw *= M;
cnt[h]++;
}
}
while (j < int(Q.size())) {
int p, H, id;
std::tie(p, H, id) = Q[j];
if (H < 0) {
ans[id] -= cnt[-H];
} else {
ans[id] += cnt[H];
}
j++;
}
for (int i = 0; i < m; i++) {
std::cout << ans[i] << "\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
308 KB |
Output is correct |
7 |
Correct |
1 ms |
312 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1593 ms |
81168 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
3648 KB |
Output is correct |
2 |
Correct |
28 ms |
2628 KB |
Output is correct |
3 |
Correct |
35 ms |
3408 KB |
Output is correct |
4 |
Correct |
29 ms |
3188 KB |
Output is correct |
5 |
Correct |
31 ms |
2584 KB |
Output is correct |
6 |
Correct |
35 ms |
3440 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
308 KB |
Output is correct |
7 |
Correct |
1 ms |
312 KB |
Output is correct |
8 |
Execution timed out |
1593 ms |
81168 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |