#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define sz(x) (int)x.size()
#define ALL(v) v.begin(),v.end()
#define MASK(k) (1LL << (k))
#define BIT(x, i) (((x) >> (i)) & 1)
#define oo (ll)1e18
#define INF (ll)1e9
#define MOD (ll)(998244353)
using namespace std;
template<class T1, class T2>
bool maximize(T1 &a, T2 b){if(a < b){a = b; return true;} return false;}
template<class T1, class T2>
bool minimize(T1 &a, T2 b){if(a > b){a = b; return true;} return false;}
template<class T1, class T2>
void add(T1 &a, T2 b){a += b; if(a >= MOD) a -= MOD;}
template<class T1, class T2>
void sub(T1 &a, T2 b){a -= b; if(a < 0) a += MOD;}
template<class T>
void cps(T &v){sort(ALL(v)); v.resize(unique(ALL(v)) - v.begin());}
const int lim = 1e6 + 10, MAX = 1e5 + 10;
int n[lim][26];
vector<int> idx[lim];
int timer;
int N, Q;
string a[MAX];
void solve(){
memset(n, -1, sizeof n);
cin >> N >> Q;
for(int i = 1; i <= N; i++) cin >> a[i];
sort(a + 1, a + N + 1);
for(int i = 1; i <= N; i++){
int p = 0;
for(int j = sz(a[i]) - 1; j >= 0; j--){
int id = a[i][j] - 'A';
if(n[p][id] == -1) n[p][id] = ++timer;
p = n[p][id];
idx[p].push_back(i);
}
}
while(Q--){
string x, y; cin >> x >> y;
int l = lower_bound(a + 1, a + N + 1, x) - a;
x.back()++;
int r = lower_bound(a + 1, a + N + 1, x) - a - 1;
if(l > r){
cout << 0 << '\n';
continue;
}
int p = 0;
for(int j = sz(y) - 1; j >= 0; j--){
int id = y[j] - 'A';
p = n[p][id];
if(p == -1) break;
}
if(p == -1) cout << 0 << '\n';
else{
int pl = lower_bound(ALL(idx[p]), l) - idx[p].begin();
int pr = upper_bound(ALL(idx[p]), r) - idx[p].begin();
cout << pr - pl << '\n';
}
}
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
// freopen("FBUY.inp","r",stdin);
// freopen("FBUY.out","w",stdout);
solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
56 ms |
128596 KB |
Output is correct |
2 |
Correct |
63 ms |
128592 KB |
Output is correct |
3 |
Correct |
59 ms |
128760 KB |
Output is correct |
4 |
Correct |
60 ms |
128592 KB |
Output is correct |
5 |
Correct |
59 ms |
128592 KB |
Output is correct |
6 |
Correct |
59 ms |
128592 KB |
Output is correct |
7 |
Correct |
68 ms |
128688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
222 ms |
330648 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
82 ms |
130128 KB |
Output is correct |
2 |
Correct |
75 ms |
129880 KB |
Output is correct |
3 |
Correct |
77 ms |
129868 KB |
Output is correct |
4 |
Correct |
74 ms |
129872 KB |
Output is correct |
5 |
Correct |
76 ms |
129872 KB |
Output is correct |
6 |
Correct |
81 ms |
129744 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
56 ms |
128596 KB |
Output is correct |
2 |
Correct |
63 ms |
128592 KB |
Output is correct |
3 |
Correct |
59 ms |
128760 KB |
Output is correct |
4 |
Correct |
60 ms |
128592 KB |
Output is correct |
5 |
Correct |
59 ms |
128592 KB |
Output is correct |
6 |
Correct |
59 ms |
128592 KB |
Output is correct |
7 |
Correct |
68 ms |
128688 KB |
Output is correct |
8 |
Runtime error |
222 ms |
330648 KB |
Execution killed with signal 11 |
9 |
Halted |
0 ms |
0 KB |
- |