#include <bits/stdc++.h>
#define faster ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define pii pair<int, int>
#define fi first
#define se second
#define endl '\n'
#define int long long
const int maxn = 1e6 + 6;
const int INF = 1e9;
using namespace std;
int n, m;
string s[maxn];
int get_val(char ch) {
return (ch == 'A' ? 1 : ch == 'G' ? 2 : ch == 'C' ? 3 : 4);
}
struct Trie {
struct Node {
Node* child[5];
int l, r;
int exits;
Node() {
fill(begin(child), end(child), nullptr);
l = INF;
r = -INF;
exits = 0;
}
};
Node* root;
Trie() {
root = new Node();
};
void add(string s, int id) {
Node* p = root;
for (char c : s) {
int val = get_val(c);
if (p->child[val] == nullptr) p->child[val] = new Node;
p = p->child[val];
p->l = min(p->l, id);
p->r = max(p->r, id);
}
p->exits++;
}
pii get_range(string s) {
Node* p = root;
for (char c : s) {
int val = get_val(c);
if (p->child[val] == nullptr) return {-1, -1};
p = p->child[val];
}
return {p->l, p->r};
}
};
struct RTrie {
struct Node {
Node* child[5];
vector<int> exits;
Node() {
fill(begin(child), end(child), nullptr);
}
};
Node* root;
RTrie() {
root = new Node;
};
void add(string s, int id) {
Node* p = root;
for (char c : s) {
int val = get_val(c);
if (p->child[val] == nullptr) p->child[val] = new Node;
p = p->child[val];
p->exits.push_back(id);
}
}
int get_ans(string s, int u, int v) {
Node* p = root;
for (char c : s) {
int val = get_val(c);
if (p->child[val] == nullptr) return 0;
p = p->child[val];
}
// Chỉ cần sắp xếp lại nếu vector chưa được sắp xếp
if (!is_sorted(p->exits.begin(), p->exits.end())) {
sort(p->exits.begin(), p->exits.end());
}
int l = lower_bound(p->exits.begin(), p->exits.end(), u) - p->exits.begin();
int r = upper_bound(p->exits.begin(), p->exits.end(), v) - p->exits.begin() - 1;
return r - l + 1;
}
};
void solve() {
cin >> n >> m;
for (int i = 1; i <= n; ++i) cin >> s[i];
sort(s + 1, s + n + 1);
Trie tr;
RTrie Rtr;
for (int i = 1; i <= n; ++i) {
tr.add(s[i], i);
reverse(s[i].begin(), s[i].end());
Rtr.add(s[i], i);
}
while (m--) {
string a, b;
cin >> a >> b;
pii it = tr.get_range(a);
reverse(b.begin(), b.end());
if (it.fi == -1) cout << 0 << '\n';
else cout << Rtr.get_ans(b, it.fi, it.se) << '\n';
}
}
int32_t main() {
faster;
solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
31576 KB |
Output is correct |
2 |
Correct |
11 ms |
31576 KB |
Output is correct |
3 |
Correct |
11 ms |
31580 KB |
Output is correct |
4 |
Correct |
11 ms |
31580 KB |
Output is correct |
5 |
Correct |
11 ms |
31580 KB |
Output is correct |
6 |
Correct |
14 ms |
31580 KB |
Output is correct |
7 |
Correct |
11 ms |
31772 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
206 ms |
253508 KB |
Output is correct |
2 |
Correct |
185 ms |
242352 KB |
Output is correct |
3 |
Correct |
166 ms |
210324 KB |
Output is correct |
4 |
Correct |
142 ms |
201896 KB |
Output is correct |
5 |
Correct |
175 ms |
264276 KB |
Output is correct |
6 |
Correct |
167 ms |
267604 KB |
Output is correct |
7 |
Correct |
75 ms |
54612 KB |
Output is correct |
8 |
Correct |
186 ms |
186004 KB |
Output is correct |
9 |
Correct |
137 ms |
164436 KB |
Output is correct |
10 |
Correct |
130 ms |
158772 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
655 ms |
33308 KB |
Output is correct |
2 |
Correct |
100 ms |
33948 KB |
Output is correct |
3 |
Correct |
139 ms |
33616 KB |
Output is correct |
4 |
Correct |
168 ms |
33228 KB |
Output is correct |
5 |
Correct |
238 ms |
33768 KB |
Output is correct |
6 |
Correct |
363 ms |
33328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
31576 KB |
Output is correct |
2 |
Correct |
11 ms |
31576 KB |
Output is correct |
3 |
Correct |
11 ms |
31580 KB |
Output is correct |
4 |
Correct |
11 ms |
31580 KB |
Output is correct |
5 |
Correct |
11 ms |
31580 KB |
Output is correct |
6 |
Correct |
14 ms |
31580 KB |
Output is correct |
7 |
Correct |
11 ms |
31772 KB |
Output is correct |
8 |
Correct |
206 ms |
253508 KB |
Output is correct |
9 |
Correct |
185 ms |
242352 KB |
Output is correct |
10 |
Correct |
166 ms |
210324 KB |
Output is correct |
11 |
Correct |
142 ms |
201896 KB |
Output is correct |
12 |
Correct |
175 ms |
264276 KB |
Output is correct |
13 |
Correct |
167 ms |
267604 KB |
Output is correct |
14 |
Correct |
75 ms |
54612 KB |
Output is correct |
15 |
Correct |
186 ms |
186004 KB |
Output is correct |
16 |
Correct |
137 ms |
164436 KB |
Output is correct |
17 |
Correct |
130 ms |
158772 KB |
Output is correct |
18 |
Correct |
655 ms |
33308 KB |
Output is correct |
19 |
Correct |
100 ms |
33948 KB |
Output is correct |
20 |
Correct |
139 ms |
33616 KB |
Output is correct |
21 |
Correct |
168 ms |
33228 KB |
Output is correct |
22 |
Correct |
238 ms |
33768 KB |
Output is correct |
23 |
Correct |
363 ms |
33328 KB |
Output is correct |
24 |
Correct |
190 ms |
215632 KB |
Output is correct |
25 |
Correct |
213 ms |
215632 KB |
Output is correct |
26 |
Correct |
179 ms |
213520 KB |
Output is correct |
27 |
Correct |
206 ms |
180128 KB |
Output is correct |
28 |
Correct |
816 ms |
80240 KB |
Output is correct |
29 |
Correct |
193 ms |
44864 KB |
Output is correct |