#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2,fma")
//#pragma expected_value
//#pragma isolated_call
//#pragma disjoint
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ll long long
#define ull unsigned long long
#define ld long double
#define pb push_back
#define setpre(i) setprecision(i)<<fixed
#define foru(i, a, b) for(int i=a;i<=b;++i)
#define ford(i, a, b) for(int i=a;i>=b;--i)
#define mp make_pair
#define moo cout<<"moo "
#define fi first
#define se second
ll lmod = 1e9 + 7;
typedef vector<int> vi;
typedef vector< vector<int> > vvi;
typedef pair<int, int> ii;
typedef pair<double, double> dd;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
vector<string> s;
int getid(char c){
if(c == 'A') return 0;
if(c == 'U') return 1;
if(c == 'G') return 2;
return 3;
}
struct node{
int next[4];
int l, r;
node(){
memset(next, -1, sizeof next);
l = INT_MAX;
r = -1;
}
};
struct node2{
int next[4];
ordered_set os;
node2(){
memset(next, -1, sizeof next);
}
};
struct {
vector<node> t;
void init(){
t.emplace_back();
}
void insert(int id){
int ptr = 0;
for(char c: s[id]){
if(t[ptr].next[getid(c)] == -1){
t.emplace_back();
t[ptr].next[getid(c)] = t.size() - 1;
}
ptr = t[ptr].next[getid(c)];
t[ptr].l = min(t[ptr].l, id);
t[ptr].r = max(t[ptr].l, id);
}
}
int traverse(string z){
int ptr = 0;
for(char c: z){
if(t[ptr].next[getid(c)] == -1) return -1;
ptr = t[ptr].next[getid(c)];
}
return ptr;
}
} trie1;
struct {
vector<node2> t;
void init(){
t.emplace_back();
}
void insert(int id){
int ptr = 0;
for(char c: s[id]){
if(t[ptr].next[getid(c)] == -1){
t.emplace_back();
t[ptr].next[getid(c)] = t.size() - 1;
}
ptr = t[ptr].next[getid(c)];
t[ptr].os.insert(id);
}
}
int traverse(string z){
int ptr = 0;
for(char c: z){
if(t[ptr].next[getid(c)] == -1) return -1;
ptr = t[ptr].next[getid(c)];
}
return ptr;
}
} trie2;
int main(){
// freopen("input.inp", "r", stdin);
// freopen("output.out", "w", stdout);
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n, q; cin >> n >> q;
foru(i, 1, n){
string z; cin >> z;
s.pb(z);
}
trie1.init(); trie2.init();
sort(s.begin(), s.end());
// for(auto z: s) cout << z << endl;
foru(i, 0, n - 1) {
trie1.insert(i);
reverse(s[i].begin(), s[i].end());
trie2.insert(i);
}
while(q--){
string z, t; cin >> z >> t;
reverse(t.begin(), t.end());
int a = trie1.traverse(z);
int b = trie2.traverse(t);
if(a == -1 or b == -1){
cout << 0 << endl; continue;
}
// for(int i: trie2.t[b].os) cout << i << " ";
// cout << endl;
int l = trie1.t[a].l;
int r = trie1.t[a].r;
// moo << l << " " << r << endl;
int zz = trie2.t[b].os.order_of_key(r + 1) - trie2.t[b].os.order_of_key(l);
cout << max(zz, 0) << endl;
}
// fclose(stdout);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
472 ms |
302256 KB |
Output is correct |
2 |
Correct |
510 ms |
307980 KB |
Output is correct |
3 |
Correct |
859 ms |
146512 KB |
Output is correct |
4 |
Correct |
1006 ms |
144380 KB |
Output is correct |
5 |
Correct |
455 ms |
325404 KB |
Output is correct |
6 |
Correct |
459 ms |
325352 KB |
Output is correct |
7 |
Correct |
856 ms |
77904 KB |
Output is correct |
8 |
Correct |
700 ms |
202796 KB |
Output is correct |
9 |
Correct |
824 ms |
237808 KB |
Output is correct |
10 |
Correct |
1117 ms |
287768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
109 ms |
7220 KB |
Output is correct |
2 |
Correct |
75 ms |
10284 KB |
Output is correct |
3 |
Correct |
85 ms |
9276 KB |
Output is correct |
4 |
Correct |
79 ms |
6620 KB |
Output is correct |
5 |
Correct |
72 ms |
6504 KB |
Output is correct |
6 |
Correct |
95 ms |
10112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
316 KB |
Output is correct |
8 |
Correct |
472 ms |
302256 KB |
Output is correct |
9 |
Correct |
510 ms |
307980 KB |
Output is correct |
10 |
Correct |
859 ms |
146512 KB |
Output is correct |
11 |
Correct |
1006 ms |
144380 KB |
Output is correct |
12 |
Correct |
455 ms |
325404 KB |
Output is correct |
13 |
Correct |
459 ms |
325352 KB |
Output is correct |
14 |
Correct |
856 ms |
77904 KB |
Output is correct |
15 |
Correct |
700 ms |
202796 KB |
Output is correct |
16 |
Correct |
824 ms |
237808 KB |
Output is correct |
17 |
Correct |
1117 ms |
287768 KB |
Output is correct |
18 |
Correct |
109 ms |
7220 KB |
Output is correct |
19 |
Correct |
75 ms |
10284 KB |
Output is correct |
20 |
Correct |
85 ms |
9276 KB |
Output is correct |
21 |
Correct |
79 ms |
6620 KB |
Output is correct |
22 |
Correct |
72 ms |
6504 KB |
Output is correct |
23 |
Correct |
95 ms |
10112 KB |
Output is correct |
24 |
Correct |
602 ms |
323440 KB |
Output is correct |
25 |
Correct |
616 ms |
324064 KB |
Output is correct |
26 |
Correct |
576 ms |
324872 KB |
Output is correct |
27 |
Correct |
1361 ms |
216652 KB |
Output is correct |
28 |
Correct |
781 ms |
153112 KB |
Output is correct |
29 |
Correct |
520 ms |
54168 KB |
Output is correct |