#include <bits/stdc++.h>
using namespace std;
template<class T> bool ckmin(T &u, const T &v) { return u > v ? (u = v, true) : false; }
template<class T> bool ckmax(T &u, const T &v) { return u < v ? (u = v, true) : false; }
int conv(char c) {
if (c == 'A') return 0;
if (c == 'U') return 1;
if (c == 'G') return 2;
return 3;
}
struct prefix_trie {
typedef array<int, 4> node;
vector<node> tree;
vector<pair<int, int>> range;
prefix_trie() : tree(1, node{}), range(1, {int(1e9), -1}) {}
void insert(const string &s, int idx) {
int cur = 0;
for (int i = 0; i < (int)s.size(); i++) {
int c = conv(s[i]);
if (!tree[cur][c]) {
tree[cur][c] = (int)tree.size();
tree.push_back(node{});
range.push_back({int(1e9), -1});
}
cur = tree[cur][c];
ckmin(range[cur].first, idx);
ckmax(range[cur].second, idx);
}
}
pair<int, int> query(const string &s) {
int cur = 0;
for (int i = 0; i < (int)s.size(); i++) {
int c = conv(s[i]);
if (!tree[cur][c]) return {-1, -1};
cur = tree[cur][c];
}
return range[cur];
};
};
struct suffix_trie {
typedef array<int, 4> node;
vector<node> tree;
vector<vector<int>> indices;
suffix_trie() : tree(1, node{}), indices(1) {}
void insert(const string &s, int idx) {
int cur = 0;
for (int i = 0; i < (int)s.size(); i++) {
int c = conv(s[i]);
if (!tree[cur][c]) {
tree[cur][c] = (int)tree.size();
tree.push_back(node{});
indices.push_back(vector<int>());
}
cur = tree[cur][c];
indices[cur].push_back(idx);
}
}
vector<int> query(const string &s) {
int cur = 0;
for (int i = 0; i < (int)s.size(); i++) {
int c = conv(s[i]);
if (!tree[cur][c]) return vector<int>();
cur = tree[cur][c];
}
return indices[cur];
}
};
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int n, m; cin >> n >> m;
string word_list[n]; for (int i = 0; i < n; i++) cin >> word_list[i];
sort(word_list, word_list + n);
prefix_trie pfx;
suffix_trie sfx;
for (int i = 0; i < n; i++) {
pfx.insert(word_list[i], i);
reverse(word_list[i].begin(), word_list[i].end());
sfx.insert(word_list[i], i);
}
for (int i = 0; i < m; i++) {
string p, q; cin >> p >> q;
reverse(q.begin(), q.end());
auto t1 = pfx.query(p);
auto t2 = sfx.query(q);
auto j1 = lower_bound(t2.begin(), t2.end(), t1.first);
auto j2 = upper_bound(t2.begin(), t2.end(), t1.second);
cout << int(j2 - j1) << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 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 |
181 ms |
144956 KB |
Output is correct |
2 |
Correct |
188 ms |
137680 KB |
Output is correct |
3 |
Correct |
88 ms |
68460 KB |
Output is correct |
4 |
Correct |
75 ms |
66172 KB |
Output is correct |
5 |
Correct |
176 ms |
126452 KB |
Output is correct |
6 |
Correct |
176 ms |
126428 KB |
Output is correct |
7 |
Correct |
36 ms |
15908 KB |
Output is correct |
8 |
Correct |
134 ms |
85372 KB |
Output is correct |
9 |
Correct |
119 ms |
73640 KB |
Output is correct |
10 |
Correct |
104 ms |
76032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
107 ms |
3128 KB |
Output is correct |
2 |
Correct |
23 ms |
2520 KB |
Output is correct |
3 |
Correct |
33 ms |
2832 KB |
Output is correct |
4 |
Correct |
30 ms |
2324 KB |
Output is correct |
5 |
Correct |
42 ms |
2504 KB |
Output is correct |
6 |
Correct |
62 ms |
2796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 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 |
181 ms |
144956 KB |
Output is correct |
9 |
Correct |
188 ms |
137680 KB |
Output is correct |
10 |
Correct |
88 ms |
68460 KB |
Output is correct |
11 |
Correct |
75 ms |
66172 KB |
Output is correct |
12 |
Correct |
176 ms |
126452 KB |
Output is correct |
13 |
Correct |
176 ms |
126428 KB |
Output is correct |
14 |
Correct |
36 ms |
15908 KB |
Output is correct |
15 |
Correct |
134 ms |
85372 KB |
Output is correct |
16 |
Correct |
119 ms |
73640 KB |
Output is correct |
17 |
Correct |
104 ms |
76032 KB |
Output is correct |
18 |
Correct |
107 ms |
3128 KB |
Output is correct |
19 |
Correct |
23 ms |
2520 KB |
Output is correct |
20 |
Correct |
33 ms |
2832 KB |
Output is correct |
21 |
Correct |
30 ms |
2324 KB |
Output is correct |
22 |
Correct |
42 ms |
2504 KB |
Output is correct |
23 |
Correct |
62 ms |
2796 KB |
Output is correct |
24 |
Correct |
184 ms |
120128 KB |
Output is correct |
25 |
Correct |
196 ms |
120272 KB |
Output is correct |
26 |
Correct |
180 ms |
118608 KB |
Output is correct |
27 |
Correct |
91 ms |
60652 KB |
Output is correct |
28 |
Correct |
232 ms |
33752 KB |
Output is correct |
29 |
Correct |
75 ms |
11860 KB |
Output is correct |