#include <iostream>
#include <vector>
#include <algorithm>
#include <unordered_map>
#include <cstring>
using namespace std;
unordered_map<char, int> id = {{'A', 0}, {'C', 1}, {'G', 2}, {'U', 3}};
struct Node {
int children[4];
vector<int> e;
Node() {
memset(children, -1, sizeof(children));
}
};
struct Trie {
vector<Node> trie;
Trie() {
trie = {Node()};
}
void insert(const string& s, int x) {
int node = 0, sz = s.size();
trie[node].e.push_back(x);
for (int i = 0; i < sz; i++) {
if (trie[node].children[id[s[i]]] == -1) {
trie[node].children[id[s[i]]] = trie.size();
trie.push_back(Node());
}
node = trie[node].children[id[s[i]]];
trie[node].e.push_back(x);
}
}
pair<int, int> getRange(const string& s) {
int node = 0, sz = s.size();
for (int i = 0; i < sz; i++) {
if (trie[node].children[id[s[i]]] == -1) return {-1, -1};
node = trie[node].children[id[s[i]]];
}
return {trie[node].e.front(), trie[node].e.back()};
}
int countElementsInRange(const string& s, pair<int, int> p) {
int l = p.first, r = p.second;
int node = 0, sz = s.size();
for (int i = 0; i < sz; i++) {
if (trie[node].children[id[s[i]]] == -1) return 0;
node = trie[node].children[id[s[i]]];
}
return upper_bound(trie[node].e.begin(), trie[node].e.end(), r) - lower_bound(trie[node].e.begin(), trie[node].e.end(), l);
}
};
int n, m;
vector<pair<string, string>> s;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
s.resize(n);
for (int i = 0; i < n; i++) {
cin >> s[i].first;
s[i].second = s[i].first;
reverse(s[i].second.begin(), s[i].second.end());
}
sort(s.begin(), s.end(), [](const pair<string, string>& p1, const pair<string, string>& p2) {
return p1.first < p2.first;
});
Trie trie_pref = Trie();
Trie trie_suf = Trie();
for (int i = 0; i < n; i++) {
trie_pref.insert(s[i].first, i);
trie_suf.insert(s[i].second, i);
}
for (int i = 0; i < m; i++) {
string pref, suf;
cin >> pref >> suf;
reverse(suf.begin(), suf.end());
pair<int, int> range = trie_pref.getRange(pref);
if (range.first == -1) {
cout << 0 << '\n';
} else {
cout << trie_suf.countElementsInRange(suf, range) << '\n';
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
374 ms |
154644 KB |
Output is correct |
2 |
Correct |
374 ms |
146860 KB |
Output is correct |
3 |
Correct |
379 ms |
152416 KB |
Output is correct |
4 |
Correct |
400 ms |
145584 KB |
Output is correct |
5 |
Correct |
375 ms |
192392 KB |
Output is correct |
6 |
Correct |
385 ms |
192412 KB |
Output is correct |
7 |
Correct |
213 ms |
20520 KB |
Output is correct |
8 |
Correct |
392 ms |
128176 KB |
Output is correct |
9 |
Correct |
389 ms |
115160 KB |
Output is correct |
10 |
Correct |
320 ms |
110860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
5320 KB |
Output is correct |
2 |
Correct |
27 ms |
4208 KB |
Output is correct |
3 |
Correct |
37 ms |
4988 KB |
Output is correct |
4 |
Correct |
30 ms |
4292 KB |
Output is correct |
5 |
Correct |
29 ms |
4212 KB |
Output is correct |
6 |
Correct |
33 ms |
4900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
374 ms |
154644 KB |
Output is correct |
9 |
Correct |
374 ms |
146860 KB |
Output is correct |
10 |
Correct |
379 ms |
152416 KB |
Output is correct |
11 |
Correct |
400 ms |
145584 KB |
Output is correct |
12 |
Correct |
375 ms |
192392 KB |
Output is correct |
13 |
Correct |
385 ms |
192412 KB |
Output is correct |
14 |
Correct |
213 ms |
20520 KB |
Output is correct |
15 |
Correct |
392 ms |
128176 KB |
Output is correct |
16 |
Correct |
389 ms |
115160 KB |
Output is correct |
17 |
Correct |
320 ms |
110860 KB |
Output is correct |
18 |
Correct |
32 ms |
5320 KB |
Output is correct |
19 |
Correct |
27 ms |
4208 KB |
Output is correct |
20 |
Correct |
37 ms |
4988 KB |
Output is correct |
21 |
Correct |
30 ms |
4292 KB |
Output is correct |
22 |
Correct |
29 ms |
4212 KB |
Output is correct |
23 |
Correct |
33 ms |
4900 KB |
Output is correct |
24 |
Correct |
412 ms |
132848 KB |
Output is correct |
25 |
Correct |
390 ms |
133080 KB |
Output is correct |
26 |
Correct |
396 ms |
131540 KB |
Output is correct |
27 |
Correct |
394 ms |
131164 KB |
Output is correct |
28 |
Correct |
291 ms |
49104 KB |
Output is correct |
29 |
Correct |
199 ms |
20524 KB |
Output is correct |