#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define ull unsigned long long
#define pb push_back
#define pll pair<ll, ll>
#define pii pair<int, int>
#define fi first
#define se second
using namespace std;
const int N = 2e5+5;
const int M = 450;
const ll inf = 1e15;
const ll mod = 1e9+7;
const ll mod1 = 1e9+3;
const int base = 311;
const int base1 = 97;
int n, k, m, lab[N], a[N], b[N], c[N], d[N], t, cnt;
ll ans, tong;
string s[N];
struct node
{
int x[4];
vector<int> val;
node(){}
node(int t)
{
memset(x, t, sizeof(x));
}
};
char ch[4] = {'A', 'U', 'G', 'C'};
vector<node> q;
void add(int id)
{
int i = 0;
for(int j = 0; j < (int)s[id].length(); j ++)
{
int k = 0;
for(int p = 0; p < 4; p ++)
{
if(ch[p] == s[id][j])
{
k = p;
break;
}
}
if(q[i].x[k] == 0)
{
q[i].x[k] = q.size();
q.pb(node(0));
}
i = q[i].x[k];
q[i].val.pb(id);
}
}
int get(string s, int l, int r)
{
if(l > r)return 0;
int i = 0;
for(int j = 0; j < (int)s.length(); j ++)
{
int k = 0;
for(int p = 0; p < 4; p ++)
{
if(ch[p] == s[j])
{
k = p;
break;
}
}
if(q[i].x[k] == 0)return 0;
i = q[i].x[k];
}
return upper_bound(q[i].val.begin(), q[i].val.end(), r) - lower_bound(q[i].val.begin(), q[i].val.end(), l);
}
struct IT
{
int n;
vector<int> st, lazy;
IT(int _n)
{
n = _n;
st.resize(n*4+4, 0);
lazy.resize(n*4+4, 0);
}
void down(int id, int l, int r, int mid)
{
if(lazy[id] == 0)return;
st[id<<1] = (mid-l+1) - st[id<<1];
st[id<<1|1] = (r-mid) - st[id<<1|1];
lazy[id<<1] ^= lazy[id];
lazy[id<<1|1] ^= lazy[id];
lazy[id] = 0;
}
void update(int id, int l, int r, int u, int v)
{
if(u <= l && r <= v)
{
st[id] = (r-l+1) - st[id];
lazy[id] ^= 1;
}
if(u > r || l > v)return;
int mid = (l+r)>>1;
down(id, l, r, mid);
update(id<<1, l, mid, u, v);
update(id<<1|1, mid+1, r, u, v);
st[id] = st[id<<1]+st[id<<1|1];
}
void update(int l, int r)
{
update(1, 1, n, l, r);
}
int get(int id, int l, int r, int u, int v)
{
if(u <= l && r <= v)return st[id];
if(u > r || l > v)return 0;
int mid = (l+r)>>1;
down(id, l, r, mid);
return get(id<<1, l, mid, u, v) + get(id<<1|1, mid+1, r, u, v);
}
int get(int l, int r)
{
return get(1, 1, n, l ,r);
}
};
vector<int> vi;
bool strmin(string x, string y)
{
for(int i = 0; i < (int)min(x.length(), y.length()); i ++)
{
if(x[i] < y[i])return true;
if(x[i] > y[i])return false;
}
if(x.length() >= y.length())return false;
return true;
}
bool strmax(string x, string y)
{
for(int i = 0; i < (int)min(x.length(), y.length()); i ++)
{
if(x[i] > y[i])return true;
if(x[i] < y[i])return false;
}
return false;
}
void sol()
{
cin >> n >> m;
for(int i = 1; i <= n; i ++)cin >> s[i];
sort(s+1, s+1+n);
q.pb(node(0));
for(int i = 1; i <= n; i ++)
{
//cout << s[i] << '\n';
reverse(s[i].begin(), s[i].end());
add(i);
reverse(s[i].begin(), s[i].end());
}
while(m -- > 0)
{
string pre, suf;
cin >> pre >> suf;
reverse(suf.begin(), suf.end());
int l = 1, r = n, mid;
while(l <= r)
{
mid = (l+r)>>1;
if(strmax(s[mid], pre))r = mid-1;
else l = mid+1;
}
k = r;
l = 1;
while(l <= r)
{
mid = (l+r)>>1;
if(strmin(s[mid], pre))l = mid+1;
else r = mid-1;
}
r = k;
//cout << l <<" "<<r<<'\n';
cout << get(suf, l, r) << '\n';
}
}
int main()
{
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio(0);
#define task "test"
if(fopen(task".inp", "r"))
{
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
int ntest = 1;
//cin >> ntest;
while(ntest -- > 0)
sol();
}
/*
*/
Compilation message
selling_rna.cpp: In function 'int main()':
selling_rna.cpp:193:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
193 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
selling_rna.cpp:194:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
194 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
6476 KB |
Output is correct |
2 |
Correct |
3 ms |
6476 KB |
Output is correct |
3 |
Correct |
4 ms |
6604 KB |
Output is correct |
4 |
Correct |
3 ms |
6580 KB |
Output is correct |
5 |
Correct |
3 ms |
6476 KB |
Output is correct |
6 |
Correct |
3 ms |
6476 KB |
Output is correct |
7 |
Correct |
3 ms |
6476 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
247 ms |
147084 KB |
Output is correct |
2 |
Correct |
251 ms |
139672 KB |
Output is correct |
3 |
Correct |
84 ms |
20184 KB |
Output is correct |
4 |
Correct |
74 ms |
23540 KB |
Output is correct |
5 |
Correct |
190 ms |
124256 KB |
Output is correct |
6 |
Correct |
172 ms |
124340 KB |
Output is correct |
7 |
Correct |
79 ms |
22112 KB |
Output is correct |
8 |
Correct |
178 ms |
70776 KB |
Output is correct |
9 |
Correct |
153 ms |
71332 KB |
Output is correct |
10 |
Correct |
117 ms |
73888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
61 ms |
7416 KB |
Output is correct |
2 |
Correct |
47 ms |
7484 KB |
Output is correct |
3 |
Correct |
56 ms |
7828 KB |
Output is correct |
4 |
Correct |
45 ms |
7692 KB |
Output is correct |
5 |
Correct |
44 ms |
7612 KB |
Output is correct |
6 |
Correct |
56 ms |
7716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
6476 KB |
Output is correct |
2 |
Correct |
3 ms |
6476 KB |
Output is correct |
3 |
Correct |
4 ms |
6604 KB |
Output is correct |
4 |
Correct |
3 ms |
6580 KB |
Output is correct |
5 |
Correct |
3 ms |
6476 KB |
Output is correct |
6 |
Correct |
3 ms |
6476 KB |
Output is correct |
7 |
Correct |
3 ms |
6476 KB |
Output is correct |
8 |
Correct |
247 ms |
147084 KB |
Output is correct |
9 |
Correct |
251 ms |
139672 KB |
Output is correct |
10 |
Correct |
84 ms |
20184 KB |
Output is correct |
11 |
Correct |
74 ms |
23540 KB |
Output is correct |
12 |
Correct |
190 ms |
124256 KB |
Output is correct |
13 |
Correct |
172 ms |
124340 KB |
Output is correct |
14 |
Correct |
79 ms |
22112 KB |
Output is correct |
15 |
Correct |
178 ms |
70776 KB |
Output is correct |
16 |
Correct |
153 ms |
71332 KB |
Output is correct |
17 |
Correct |
117 ms |
73888 KB |
Output is correct |
18 |
Correct |
61 ms |
7416 KB |
Output is correct |
19 |
Correct |
47 ms |
7484 KB |
Output is correct |
20 |
Correct |
56 ms |
7828 KB |
Output is correct |
21 |
Correct |
45 ms |
7692 KB |
Output is correct |
22 |
Correct |
44 ms |
7612 KB |
Output is correct |
23 |
Correct |
56 ms |
7716 KB |
Output is correct |
24 |
Correct |
276 ms |
125944 KB |
Output is correct |
25 |
Correct |
331 ms |
125924 KB |
Output is correct |
26 |
Correct |
253 ms |
125908 KB |
Output is correct |
27 |
Correct |
98 ms |
23432 KB |
Output is correct |
28 |
Correct |
398 ms |
36688 KB |
Output is correct |
29 |
Correct |
172 ms |
14988 KB |
Output is correct |