#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include <debug.h>
#else
#define debug(...)
#endif
#define ft front
#define bk back
#define st first
#define nd second
#define ins insert
#define ers erase
#define pb push_back
#define pf push_front
#define _pb pop_back
#define _pf pop_front
#define lb lower_bound
#define ub upper_bound
#define mtp make_tuple
#define bg begin
#define ed end
#define all(x) (x).bg(), (x).ed()
#define sz(x) (int)(x).size()
typedef long long ll; typedef unsigned long long ull;
typedef double db; typedef long double ldb;
typedef pair<int, int> pi; typedef pair<ll, ll> pll;
typedef vector<int> vi; typedef vector<ll> vll; typedef vector<pi> vpi; typedef vector<pll> vpll;
typedef string str;
template<typename T> T gcd(T a, T b) { return (b == 0? a : gcd(b, a % b)); }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
#define FOS(i, r, l) for (int (i) = (r); (i) >= (l); --(i))
#define FRN(i, n) for (int (i) = 0; (i) < (n); ++(i))
#define FSN(i, n) for (int (i) = (n) - 1; (i) >= 0; --(i))
#define EACH(i, x) for (auto &(i) : (x))
#define WHILE while
#define file "TEST"
mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
ll rand(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rd); }
/*
----------------------------------------------------------------
END OF TEMPLATE
----------------------------------------------------------------
Tran The Bao - ghostwriter
Training for VOI23 gold medal
----------------------------------------------------------------
DIT ME CHUYEN BAO LOC
----------------------------------------------------------------
*/
const pi M = {1e9 + 7, 1e9 + 9};
const pi base = {37, 127};
#define getid(x) (x == 'A'? 1 : x == 'G'? 2 : x == 'C'? 3 : 4)
struct Rna {
str s;
vpi h;
Rna() {}
void calh() {
h.resize(sz(s));
pi cur = {0, 0};
int n = sz(s);
FRN(i, n) {
cur.st = (1LL * cur.st * base.st % M.st + getid(s[i])) % M.st;
cur.nd = (1LL * cur.nd * base.nd % M.nd + getid(s[i])) % M.nd;
h[i] = cur;
}
}
};
struct Query {
str p, q;
pi hq;
vpi hp;
Query() {}
void calh() {
hp.resize(sz(p));
pi cur = {0, 0};
int n = sz(p);
FRN(i, n) {
cur.st = (1LL * cur.st * base.st % M.st + getid(p[i])) % M.st;
cur.nd = (1LL * cur.nd * base.nd % M.nd + getid(p[i])) % M.nd;
hp[i] = cur;
}
hq = {0, 0};
n = sz(q);
FRN(i, n) {
hq.st = (1LL * hq.st * base.st % M.st + getid(q[i])) % M.st;
hq.nd = (1LL * hq.nd * base.nd % M.nd + getid(q[i])) % M.nd;
}
}
};
const int N = 1e5 + 1;
const int TLEN = 2e6 + 1;
int n, m;
Rna a[N];
Query q[N];
pi P[N];
vpi v;
vi spos[TLEN];
int getpos(pi &x) { if (x > v.bk()) return -1; int ans = lb(all(v), x) - v.bg(); if (v[ans] != x) return -1; return ans; }
int get(vi &a, int &l, int &r) {
if (l > a.bk()) return 0;
l = lb(all(a), l) - a.bg();
if (r >= a.bk()) r = sz(a) - 1;
else r = ub(all(a), r) - a.bg() - 1;
return r - l + 1;
}
pi geth(vpi &h, int l, int r) {
pi ans;
ans.st = (h[r].st - 1LL * (l? h[l - 1].st : 0) * P[r - l + 1].st % M.st + M.st) % M.st;
ans.nd = (h[r].nd - 1LL * (l? h[l - 1].nd : 0) * P[r - l + 1].nd % M.nd + M.nd) % M.nd;
return ans;
}
int mmatch(vpi &h, vpi &h1) {
int l = 0, r = min(sz(h), sz(h1)) - 1, ans = -1;
WHILE(l <= r) {
int mid = l + (r - l) / 2;
pi f = geth(h, 0, mid), s = geth(h1, 0, mid);
if (f == s) {
ans = mid;
l = mid + 1;
}
else r = mid - 1;
}
return ans + 1;
}
bool cmp(Rna &a, Rna &b) {
int len = mmatch(a.h, b.h);
if (len == sz(a.s) && len == sz(b.s)) return 0;
if (len == sz(a.s)) return 1;
if (len == sz(b.s)) return 0;
return a.s[len] < b.s[len];
}
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
// freopen(file".inp", "r", stdin);
// freopen(file".out", "w", stdout);
P[0] = {1, 1};
FOR(i, 1, N - 1) {
P[i].st = 1LL * P[i - 1].st * base.st % M.st;
P[i].nd = 1LL * P[i - 1].nd * base.nd % M.nd;
}
cin >> n >> m;
FOR(i, 1, n) {
cin >> a[i].s;
a[i].calh();
pi cur = {0, 0};
FSN(j, sz(a[i].s)) {
cur.st = (1LL * cur.st * base.st % M.st + getid(a[i].s[j])) % M.st;
cur.nd = (1LL * cur.nd * base.nd % M.nd + getid(a[i].s[j])) % M.nd;
v.pb(cur);
}
}
FOR(i, 1, m) {
cin >> q[i].p >> q[i].q;
reverse(all(q[i].q));
q[i].calh();
}
sort(a + 1, a + 1 + n, cmp);
sort(all(v));
FOR(i, 1, n) {
pi cur = {0, 0};
FSN(j, sz(a[i].s)) {
cur.st = (1LL * cur.st * base.st % M.st + getid(a[i].s[j])) % M.st;
cur.nd = (1LL * cur.nd * base.nd % M.nd + getid(a[i].s[j])) % M.nd;
spos[getpos(cur)].pb(i);
}
}
FOR(i, 1, m) {
Query &curq = q[i];
int pos = getpos(curq.hq);
if (pos == -1) {
cout << 0 << '\n';
continue;
}
int clen = sz(curq.p), l = 1, r = n, lans = -1, uans = -1;
WHILE(l <= r) {
int mid = l + (r - l) / 2;
int len = mmatch(a[mid].h, curq.hp);
if (len == clen) {
lans = mid;
r = mid - 1;
}
else if (a[mid].s[len] < curq.p[len]) l = mid + 1;
else r = mid - 1;
}
if (lans == -1) {
cout << 0 << '\n';
continue;
}
l = 1; r = n;
WHILE(l <= r) {
int mid = l + (r - l) / 2;
int len = mmatch(a[mid].h, curq.hp);
if (len == clen) {
uans = mid;
l = mid + 1;
}
else if (a[mid].s[len] < curq.p[len]) l = mid + 1;
else r = mid - 1;
}
cout << get(spos[pos], lans, uans) << '\n';
}
return 0;
}
/*
2 3
AUGC
AGC
G C
AU C
A C
----------------------------------------------------------------
From Benq:
stuff you should look for
* int overflow, array bounds
* special cases (n=1?)
* do smth instead of nothing and stay organized
* WRITE STUFF DOWN
* DON'T GET STUCK ON ONE APPROACH
----------------------------------------------------------------
*/
Compilation message
selling_rna.cpp: In member function 'void Rna::calh()':
selling_rna.cpp:34:28: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
34 | #define FRN(i, n) for (int (i) = 0; (i) < (n); ++(i))
| ^
selling_rna.cpp:62:3: note: in expansion of macro 'FRN'
62 | FRN(i, n) {
| ^~~
selling_rna.cpp: In member function 'void Query::calh()':
selling_rna.cpp:34:28: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
34 | #define FRN(i, n) for (int (i) = 0; (i) < (n); ++(i))
| ^
selling_rna.cpp:78:3: note: in expansion of macro 'FRN'
78 | FRN(i, n) {
| ^~~
selling_rna.cpp:34:28: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
34 | #define FRN(i, n) for (int (i) = 0; (i) < (n); ++(i))
| ^
selling_rna.cpp:85:3: note: in expansion of macro 'FRN'
85 | FRN(i, n) {
| ^~~
selling_rna.cpp: In function 'int main()':
selling_rna.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
selling_rna.cpp:138:5: note: in expansion of macro 'FOR'
138 | FOR(i, 1, N - 1) {
| ^~~
selling_rna.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
selling_rna.cpp:143:5: note: in expansion of macro 'FOR'
143 | FOR(i, 1, n) {
| ^~~
selling_rna.cpp:35:28: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
35 | #define FSN(i, n) for (int (i) = (n) - 1; (i) >= 0; --(i))
| ^
selling_rna.cpp:147:6: note: in expansion of macro 'FSN'
147 | FSN(j, sz(a[i].s)) {
| ^~~
selling_rna.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
selling_rna.cpp:153:5: note: in expansion of macro 'FOR'
153 | FOR(i, 1, m) {
| ^~~
selling_rna.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
selling_rna.cpp:160:5: note: in expansion of macro 'FOR'
160 | FOR(i, 1, n) {
| ^~~
selling_rna.cpp:35:28: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
35 | #define FSN(i, n) for (int (i) = (n) - 1; (i) >= 0; --(i))
| ^
selling_rna.cpp:162:6: note: in expansion of macro 'FSN'
162 | FSN(j, sz(a[i].s)) {
| ^~~
selling_rna.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
selling_rna.cpp:168:5: note: in expansion of macro 'FOR'
168 | FOR(i, 1, m) {
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
62932 KB |
Output is correct |
2 |
Correct |
32 ms |
62944 KB |
Output is correct |
3 |
Correct |
32 ms |
62932 KB |
Output is correct |
4 |
Correct |
32 ms |
62984 KB |
Output is correct |
5 |
Correct |
31 ms |
62868 KB |
Output is correct |
6 |
Correct |
31 ms |
62852 KB |
Output is correct |
7 |
Correct |
31 ms |
62860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1596 ms |
175688 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
152 ms |
67744 KB |
Output is correct |
2 |
Correct |
131 ms |
66936 KB |
Output is correct |
3 |
Correct |
142 ms |
67416 KB |
Output is correct |
4 |
Correct |
118 ms |
66496 KB |
Output is correct |
5 |
Correct |
112 ms |
66736 KB |
Output is correct |
6 |
Correct |
137 ms |
67264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
62932 KB |
Output is correct |
2 |
Correct |
32 ms |
62944 KB |
Output is correct |
3 |
Correct |
32 ms |
62932 KB |
Output is correct |
4 |
Correct |
32 ms |
62984 KB |
Output is correct |
5 |
Correct |
31 ms |
62868 KB |
Output is correct |
6 |
Correct |
31 ms |
62852 KB |
Output is correct |
7 |
Correct |
31 ms |
62860 KB |
Output is correct |
8 |
Execution timed out |
1596 ms |
175688 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |