#include <bits/stdc++.h>
using namespace std;
inline void convert(string &s)
{
int n = s.size();
for (int i = 0; i < n; i++)
{
if (s[i] == 'G')
{
s[i] = 'B';
}
if (s[i] == 'U')
{
s[i] = 'D';
}
}
}
const int K = 4;
struct Trie
{
struct Node
{
int cnt = 0;
Node *ch[K]{};
};
Node *rt = new Node();
void add(const string &s)
{
Node *cur = rt;
int n = s.size();
for (int i = 0; i < n; i++)
{
if (cur->ch[s[i] - 'A'] == nullptr)
{
cur->ch[s[i] - 'A'] = new Node();
}
cur = cur->ch[s[i] - 'A'];
cur->cnt++;
}
}
int query(const string &s)
{
Node *cur = rt;
int n = s.size();
for (int i = 0; i < n; i++)
{
if (cur->ch[s[i] - 'A'] == nullptr)
{
return 0;
}
cur = cur->ch[s[i] - 'A'];
}
return cur->cnt;
}
};
int main()
{
cin.tie(0)->sync_with_stdio(0);
int n, q;
cin >> n >> q;
vector<string> a(n);
for (int i = 0; i < n; i++)
{
cin >> a[i];
convert(a[i]);
}
sort(a.begin(), a.end());
vector<string> b(q);
vector<vector<int>> ll(n + 1), rr(n + 1);
for (int i = 0; i < q; i++)
{
string s;
cin >> s >> b[i];
convert(s);
convert(b[i]);
reverse(b[i].begin(), b[i].end());
int l = lower_bound(a.begin(), a.end(), s) - a.begin();
s.back()++;
int r = lower_bound(a.begin(), a.end(), s) - a.begin();
ll[l].push_back(i);
rr[r].push_back(i);
}
Trie tr;
vector<int> ans(q);
for (int t = 0; t <= n; t++)
{
for (auto id : rr[t])
{
ans[id] += tr.query(b[id]);
}
for (auto id : ll[t])
{
ans[id] -= tr.query(b[id]);
}
if (t < n)
{
reverse(a[t].begin(), a[t].end());
tr.add(a[t]);
}
}
for (int i = 0; i < q; i++)
{
cout << ans[i] << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
107 ms |
94952 KB |
Output is correct |
2 |
Correct |
117 ms |
90232 KB |
Output is correct |
3 |
Correct |
28 ms |
5308 KB |
Output is correct |
4 |
Correct |
40 ms |
5496 KB |
Output is correct |
5 |
Correct |
66 ms |
60108 KB |
Output is correct |
6 |
Correct |
74 ms |
60980 KB |
Output is correct |
7 |
Correct |
40 ms |
4444 KB |
Output is correct |
8 |
Correct |
61 ms |
38740 KB |
Output is correct |
9 |
Correct |
51 ms |
33540 KB |
Output is correct |
10 |
Correct |
37 ms |
30800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
7004 KB |
Output is correct |
2 |
Correct |
19 ms |
4272 KB |
Output is correct |
3 |
Correct |
23 ms |
5724 KB |
Output is correct |
4 |
Correct |
17 ms |
4952 KB |
Output is correct |
5 |
Correct |
19 ms |
4188 KB |
Output is correct |
6 |
Correct |
25 ms |
5460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
107 ms |
94952 KB |
Output is correct |
9 |
Correct |
117 ms |
90232 KB |
Output is correct |
10 |
Correct |
28 ms |
5308 KB |
Output is correct |
11 |
Correct |
40 ms |
5496 KB |
Output is correct |
12 |
Correct |
66 ms |
60108 KB |
Output is correct |
13 |
Correct |
74 ms |
60980 KB |
Output is correct |
14 |
Correct |
40 ms |
4444 KB |
Output is correct |
15 |
Correct |
61 ms |
38740 KB |
Output is correct |
16 |
Correct |
51 ms |
33540 KB |
Output is correct |
17 |
Correct |
37 ms |
30800 KB |
Output is correct |
18 |
Correct |
24 ms |
7004 KB |
Output is correct |
19 |
Correct |
19 ms |
4272 KB |
Output is correct |
20 |
Correct |
23 ms |
5724 KB |
Output is correct |
21 |
Correct |
17 ms |
4952 KB |
Output is correct |
22 |
Correct |
19 ms |
4188 KB |
Output is correct |
23 |
Correct |
25 ms |
5460 KB |
Output is correct |
24 |
Correct |
84 ms |
79444 KB |
Output is correct |
25 |
Correct |
120 ms |
80724 KB |
Output is correct |
26 |
Correct |
75 ms |
81812 KB |
Output is correct |
27 |
Correct |
41 ms |
10928 KB |
Output is correct |
28 |
Correct |
124 ms |
29696 KB |
Output is correct |
29 |
Correct |
74 ms |
16536 KB |
Output is correct |