#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 = upper_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 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
76 ms |
95004 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
6912 KB |
Output is correct |
2 |
Incorrect |
19 ms |
4444 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |