#include <bits/stdc++.h>
using namespace std;
struct Pre
{
struct node
{
int l, r;
node* child[26];
node ()
{
l = 1e9, r = 0;
for (int i = 0; i < 26; i++) child[i] = NULL;
}
} *root;
Pre ()
{
root = new node();
}
void add(string s, int idx)
{
node* cur = root;
for (int i = 0; i < (int)s.size(); i++)
{
int id = s[i]-'A';
if (cur->child[id] == NULL) cur->child[id] = new node();
cur = cur->child[id];
cur->l = min(cur->l, idx), cur->r = max(cur->r, idx);
}
}
pair<int, int> get(string s)
{
node* cur = root;
for (int i = 0; i < (int)s.size(); i++)
{
int id = s[i]-'A';
if (cur->child[id] == NULL) return {-1, -1};
cur = cur->child[id];
}
return {cur->l, cur->r};
}
} pre;
struct Suf
{
struct node
{
vector<int> v;
node* child[26];
node ()
{
for (int i = 0; i < 26; i++) child[i] = NULL;
}
} *root;
Suf ()
{
root = new node();
}
void add(string s, int idx)
{
reverse(s.begin(), s.end());
node* cur = root;
for (int i = 0; i < (int)s.size(); i++)
{
int id = s[i]-'A';
if (cur->child[id] == NULL) cur->child[id] = new node();
cur = cur->child[id];
cur->v.push_back(idx);
}
}
void srt(node* cur = NULL)
{
if (cur == NULL) cur = root;
sort(cur->v.begin(), cur->v.end());
for (int i = 0; i < 26; i++)
if (cur->child[i] != NULL) srt(cur->child[i]);
}
int get(string s, int l, int r)
{
reverse(s.begin(), s.end());
node* cur = root;
for (int i = 0; i < (int)s.size(); i++)
{
int id = s[i]-'A';
if (cur->child[id] == NULL) return 0;
cur = cur->child[id];
}
int L = lower_bound(cur->v.begin(), cur->v.end(), l)-cur->v.begin();
int R = upper_bound(cur->v.begin(), cur->v.end(), r)-cur->v.begin();
return R-L;
}
} suf;
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
vector<string> s(n);
for (int i = 0; i < n; i++) cin >> s[i];
sort(s.begin(), s.end());
for (int i = 0; i < n; i++) pre.add(s[i], i), suf.add(s[i], i);
suf.srt();
while (m--)
{
string p, q;
cin >> p >> q;
pair<int, int> pr = pre.get(p);
if (pr.first == -1) cout << "0\n";
else cout << suf.get(q, pr.first, pr.second) << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
395 ms |
526932 KB |
Output is correct |
2 |
Correct |
387 ms |
498932 KB |
Output is correct |
3 |
Correct |
295 ms |
439888 KB |
Output is correct |
4 |
Correct |
261 ms |
418700 KB |
Output is correct |
5 |
Correct |
451 ms |
592184 KB |
Output is correct |
6 |
Correct |
446 ms |
601172 KB |
Output is correct |
7 |
Correct |
59 ms |
11344 KB |
Output is correct |
8 |
Correct |
288 ms |
354792 KB |
Output is correct |
9 |
Correct |
295 ms |
298456 KB |
Output is correct |
10 |
Correct |
241 ms |
289660 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
2900 KB |
Output is correct |
2 |
Correct |
15 ms |
3160 KB |
Output is correct |
3 |
Correct |
18 ms |
2904 KB |
Output is correct |
4 |
Correct |
13 ms |
2140 KB |
Output is correct |
5 |
Correct |
15 ms |
3164 KB |
Output is correct |
6 |
Correct |
18 ms |
2908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
395 ms |
526932 KB |
Output is correct |
9 |
Correct |
387 ms |
498932 KB |
Output is correct |
10 |
Correct |
295 ms |
439888 KB |
Output is correct |
11 |
Correct |
261 ms |
418700 KB |
Output is correct |
12 |
Correct |
451 ms |
592184 KB |
Output is correct |
13 |
Correct |
446 ms |
601172 KB |
Output is correct |
14 |
Correct |
59 ms |
11344 KB |
Output is correct |
15 |
Correct |
288 ms |
354792 KB |
Output is correct |
16 |
Correct |
295 ms |
298456 KB |
Output is correct |
17 |
Correct |
241 ms |
289660 KB |
Output is correct |
18 |
Correct |
18 ms |
2900 KB |
Output is correct |
19 |
Correct |
15 ms |
3160 KB |
Output is correct |
20 |
Correct |
18 ms |
2904 KB |
Output is correct |
21 |
Correct |
13 ms |
2140 KB |
Output is correct |
22 |
Correct |
15 ms |
3164 KB |
Output is correct |
23 |
Correct |
18 ms |
2908 KB |
Output is correct |
24 |
Correct |
462 ms |
430772 KB |
Output is correct |
25 |
Correct |
353 ms |
430516 KB |
Output is correct |
26 |
Correct |
341 ms |
425556 KB |
Output is correct |
27 |
Correct |
215 ms |
360272 KB |
Output is correct |
28 |
Correct |
146 ms |
70224 KB |
Output is correct |
29 |
Correct |
60 ms |
8772 KB |
Output is correct |