//#include"holiday.h"
#include <bits/stdc++.h>
#define ll long long
#define rep(i,m,n) for(int i=(m); i<=(n); i++)
#define reb(i,m,n) for(int i=(m); i>=(n); i--)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define MP make_pair
#define fs first
#define se second
#define bit(msk, i) ((msk >> i) & 1)
#define iter(id, v) for(auto id : v)
#define pb push_back
#define SZ(v) (ll)v.size()
#define ALL(v) v.begin(),v.end()
using namespace std;
mt19937_64 rd(chrono :: steady_clock :: now ().time_since_epoch().count());
ll Rand (ll l, ll r) { return uniform_int_distribution<ll> (l, r) (rd); }
const int N = 1e5 + 7;
int Mod = 1e9 + 7;///lon
const int INF = 1e9;
const ll BASE = 137;
const int szBL = 450;
int n, Q;
vector<string> vec;
string Rev (string &S) {
static string res;
res = "";
reb (i, SZ(S) - 1, 0) res += S[i];
return res;
}
struct tNode {
tNode *child[26];
vector<int> vec;
tNode () {
rep (i, 0, 25) child[i] = NULL;
}
}*proot = new tNode(), *sroot = new tNode();
void Add (tNode *cur, string S, int idx) {
iter (&id, S) {
int c = id - 'A';
if (cur->child[c] == NULL) cur->child[c] = new tNode();
cur = cur->child[c];
cur->vec.pb(idx);
}
}
void dfs (tNode *cur) {
rep (i, 0, 25) {
if (cur->child[i] != NULL) {
sort (ALL(cur->child[i]->vec));
dfs(cur->child[i]);
}
}
}
pii getRange (tNode *cur, string &S) {
iter (&id, S) {
int c = id - 'A';
if (cur->child[c] == NULL) {
return MP(INF, 0);
}
cur = cur->child[c];
}
return MP(cur->vec[0], cur->vec.back());
}
int get (tNode *cur, string S, pii Range) {
if (Range.fs > Range.se) return 0;
iter (&id, S) {
int c = id - 'A';
if (cur->child[c] == NULL)
return 0;
cur = cur->child[c];
}
// cout << S <<" "<<Range.fs<<" "<<Range.se<<"\n";
int L = lower_bound(ALL(cur->vec), Range.fs) - cur->vec.begin();
int R = upper_bound(ALL(cur->vec), Range.se) - cur->vec.begin();
return R - L;
}
void solution () {
cin >> n >> Q;
vec.resize(n + 1, "");
rep (i, 1, n) {
cin >> vec[i];
}
sort (vec.begin() + 1, vec.end());
rep (i, 1, n) {
Add(proot, vec[i], i);
Add(sroot, Rev(vec[i]), i);
}
dfs(proot);
dfs(sroot);
rep (i, 1, Q) {
string T, Y;
cin >> T >> Y;
cout << get(sroot, Rev(Y), getRange(proot, T)) <<"\n";
}
}
#define file(name) freopen(name".inp","r",stdin); \
freopen(name".out","w",stdout);
int main () {
// file("c");
ios_base :: sync_with_stdio(false); cin.tie(0); cout.tie(0);
int num_Test = 1;
// cin >> num_Test;
while (num_Test--)
solution();
}
/*
no bug challenge +10
5 5
01101
10001
00110
10101
00100
*/
# |
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 |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 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 |
447 ms |
542648 KB |
Output is correct |
2 |
Correct |
443 ms |
513380 KB |
Output is correct |
3 |
Correct |
458 ms |
534452 KB |
Output is correct |
4 |
Correct |
391 ms |
509012 KB |
Output is correct |
5 |
Correct |
550 ms |
651864 KB |
Output is correct |
6 |
Correct |
582 ms |
661572 KB |
Output is correct |
7 |
Correct |
92 ms |
25172 KB |
Output is correct |
8 |
Correct |
417 ms |
400464 KB |
Output is correct |
9 |
Correct |
327 ms |
337584 KB |
Output is correct |
10 |
Correct |
310 ms |
326520 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
3664 KB |
Output is correct |
2 |
Correct |
17 ms |
4064 KB |
Output is correct |
3 |
Correct |
22 ms |
3924 KB |
Output is correct |
4 |
Correct |
17 ms |
3084 KB |
Output is correct |
5 |
Correct |
19 ms |
3932 KB |
Output is correct |
6 |
Correct |
23 ms |
3924 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 |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 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 |
447 ms |
542648 KB |
Output is correct |
9 |
Correct |
443 ms |
513380 KB |
Output is correct |
10 |
Correct |
458 ms |
534452 KB |
Output is correct |
11 |
Correct |
391 ms |
509012 KB |
Output is correct |
12 |
Correct |
550 ms |
651864 KB |
Output is correct |
13 |
Correct |
582 ms |
661572 KB |
Output is correct |
14 |
Correct |
92 ms |
25172 KB |
Output is correct |
15 |
Correct |
417 ms |
400464 KB |
Output is correct |
16 |
Correct |
327 ms |
337584 KB |
Output is correct |
17 |
Correct |
310 ms |
326520 KB |
Output is correct |
18 |
Correct |
24 ms |
3664 KB |
Output is correct |
19 |
Correct |
17 ms |
4064 KB |
Output is correct |
20 |
Correct |
22 ms |
3924 KB |
Output is correct |
21 |
Correct |
17 ms |
3084 KB |
Output is correct |
22 |
Correct |
19 ms |
3932 KB |
Output is correct |
23 |
Correct |
23 ms |
3924 KB |
Output is correct |
24 |
Correct |
384 ms |
444500 KB |
Output is correct |
25 |
Correct |
381 ms |
444668 KB |
Output is correct |
26 |
Correct |
357 ms |
439376 KB |
Output is correct |
27 |
Correct |
374 ms |
439060 KB |
Output is correct |
28 |
Correct |
195 ms |
83248 KB |
Output is correct |
29 |
Correct |
91 ms |
16596 KB |
Output is correct |