#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define ff first
#define ss second
#define pb push_back
#define rep(i, a, b) for (int i = (int)a; i <= (int)b; ++i)
#define rep1(i, a, b) for (int i = (int)a; i >= (int)b; --i)
#define all(x) x.begin(), x.end()
#define all1(x) x.begin() + 1, x.end()
const int N = 1e5 + 5;
const int inf = 1e9;
int n, m;
string s[N];
struct node1 {
node1* child[4] = {};
int left = inf, right = -inf;
};
struct node2 {
node2* child[4] = {};
vector<int> pos;
};
node1* root1 = new node1;
node2* root2 = new node2;
void add1(string ss, int idx) {
node1* cur = root1;
for (char c : ss) {
int x = c - 'a';
if (!cur -> child[x]) cur -> child[x] = new node1;
cur = cur -> child[x];
cur -> left = min(cur -> left, idx);
cur -> right = max(cur -> right, idx);
}
}
void add2(string ss, int idx) {
node2* cur = root2;
for (char c : ss) {
int x = c - 'a';
if (!cur -> child[x]) cur -> child[x] = new node2;
cur = cur -> child[x];
cur -> pos.pb(idx);
}
}
pii find1(string s) {
node1* cur = root1;
for (char c : s) {
int x = c - 'a';
if (!cur -> child[x]) return {-1, -1};
cur = cur -> child[x];
}
return {cur -> left, cur -> right};
}
int find2(string s, pii range) {
if (range.ff == -1) return 0;
vector<int> res;
node2* cur = root2;
for (char c : s) {
int x = c - 'a';
if (!cur -> child[x]) return 0;
cur = cur -> child[x];
}
auto l = lower_bound(all(cur -> pos), range.ff);
auto r = upper_bound(all(cur -> pos), range.ss);
return r - l;
}
bool cmp(string &a, string &b) {
int sz = min(a.size(), b.size());
rep(i, 0, sz - 1) {
if (a[i] != b[i]) return a[i] < b[i];
}
return a.size() < b.size();
}
void convert(string &t) {
rep(i, 0, t.size() - 1) {
if (t[i] == 'A') t[i] = 'a';
if (t[i] == 'C') t[i] = 'b';
if (t[i] == 'G') t[i] = 'c';
if (t[i] == 'U') t[i] = 'd';
}
}
void init() {
cin >> n >> m;
rep(i, 1, n) {
cin >> s[i];
convert(s[i]);
}
sort(s + 1, s + n + 1, cmp);
rep(i, 1, n) add1(s[i], i);
rep(i, 1, n) reverse(all(s[i]));
rep(i, 1, n) add2(s[i], i);
}
void solve() {
rep(_m, 1, m) {
string pref, suf; cin >> pref >> suf;
convert(pref);
convert(suf);
reverse(all(suf));
pii tmp = find1(pref);
int ans = find2(suf, tmp);
cout << ans << '\n';
}
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
init();
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
3404 KB |
Output is correct |
2 |
Correct |
2 ms |
3448 KB |
Output is correct |
3 |
Correct |
2 ms |
3408 KB |
Output is correct |
4 |
Correct |
2 ms |
3408 KB |
Output is correct |
5 |
Correct |
2 ms |
3408 KB |
Output is correct |
6 |
Correct |
2 ms |
3408 KB |
Output is correct |
7 |
Correct |
2 ms |
3408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
218 ms |
192148 KB |
Output is correct |
2 |
Correct |
225 ms |
184380 KB |
Output is correct |
3 |
Correct |
150 ms |
112028 KB |
Output is correct |
4 |
Correct |
177 ms |
107260 KB |
Output is correct |
5 |
Correct |
210 ms |
178636 KB |
Output is correct |
6 |
Correct |
215 ms |
181232 KB |
Output is correct |
7 |
Correct |
85 ms |
19052 KB |
Output is correct |
8 |
Correct |
216 ms |
117748 KB |
Output is correct |
9 |
Correct |
195 ms |
101172 KB |
Output is correct |
10 |
Correct |
139 ms |
96264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
4696 KB |
Output is correct |
2 |
Correct |
30 ms |
4816 KB |
Output is correct |
3 |
Correct |
29 ms |
4676 KB |
Output is correct |
4 |
Correct |
23 ms |
4496 KB |
Output is correct |
5 |
Correct |
24 ms |
4708 KB |
Output is correct |
6 |
Correct |
36 ms |
4652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
3404 KB |
Output is correct |
2 |
Correct |
2 ms |
3448 KB |
Output is correct |
3 |
Correct |
2 ms |
3408 KB |
Output is correct |
4 |
Correct |
2 ms |
3408 KB |
Output is correct |
5 |
Correct |
2 ms |
3408 KB |
Output is correct |
6 |
Correct |
2 ms |
3408 KB |
Output is correct |
7 |
Correct |
2 ms |
3408 KB |
Output is correct |
8 |
Correct |
218 ms |
192148 KB |
Output is correct |
9 |
Correct |
225 ms |
184380 KB |
Output is correct |
10 |
Correct |
150 ms |
112028 KB |
Output is correct |
11 |
Correct |
177 ms |
107260 KB |
Output is correct |
12 |
Correct |
210 ms |
178636 KB |
Output is correct |
13 |
Correct |
215 ms |
181232 KB |
Output is correct |
14 |
Correct |
85 ms |
19052 KB |
Output is correct |
15 |
Correct |
216 ms |
117748 KB |
Output is correct |
16 |
Correct |
195 ms |
101172 KB |
Output is correct |
17 |
Correct |
139 ms |
96264 KB |
Output is correct |
18 |
Correct |
38 ms |
4696 KB |
Output is correct |
19 |
Correct |
30 ms |
4816 KB |
Output is correct |
20 |
Correct |
29 ms |
4676 KB |
Output is correct |
21 |
Correct |
23 ms |
4496 KB |
Output is correct |
22 |
Correct |
24 ms |
4708 KB |
Output is correct |
23 |
Correct |
36 ms |
4652 KB |
Output is correct |
24 |
Correct |
229 ms |
160552 KB |
Output is correct |
25 |
Correct |
252 ms |
160816 KB |
Output is correct |
26 |
Correct |
285 ms |
158744 KB |
Output is correct |
27 |
Correct |
142 ms |
94620 KB |
Output is correct |
28 |
Correct |
235 ms |
38596 KB |
Output is correct |
29 |
Correct |
96 ms |
11912 KB |
Output is correct |