#include <bits/stdc++.h>
using namespace std;
#define TASK "long"
#define fi first
#define se second
#define ll long long
#define pb push_back
#define ALL(x) (x).begin(), (x).end()
#define GETBIT(mask, i) ((mask) >> (i) & 1)
#define MASK(i) ((1LL) << (i))
#define SZ(x) ((int)(x).size())
#define mp make_pair
#define CNTBIT(mask) __builtin_popcount(mask)
template<class X, class Y> bool maximize(X &x, Y y){ if (x < y) {x = y; return true;} return false;};
template<class X, class Y> bool minimize(X &x, Y y){ if (x > y) {x = y; return true;} return false;};
typedef pair<int, int> ii;
const int N = 1e5 + 5;
const int inf = 1e9 + 7;
const long long INF = (long long)1e18 + 7;
// const int mod = 1e9 + 7;
// void add(int &x, int y) {x += y; if (x >= mod) x -= mod;}
// void sub(int &x, int y) {x -= y; if (x < 0) x += mod;}
// int mul(int x, int y) {return 1LL * x * y % mod;}
// -----------------------------------------//
const pair<int, int> mod = {1e9 + 7, 1e9 + 9};
const pair<int, int> base = {311, 31};
pair<int, int> operator + (const ii &x, const ii &y) {
return {(x.fi + y.fi) % mod.fi, (x.se + y.se) % mod.se};
}
pair<int, int> operator - (const ii &x, const ii &y) {
return {(x.fi - y.fi + mod.fi) % mod.fi, (x.se - y.se + mod.se) % mod.se};
}
pair<int, int> operator * (const ii &x, const ii &y) {
return {1LL * x.fi * y.fi % mod.fi, 1LL * x.se * y.se % mod.se};
}
pair<int, int> pw[N];
struct Data {
string s;
int n;
vector<pair<int, int>> hs;
void init(const string &t) {
s = t;
n = SZ(s);
s = ' ' + s;
hs.assign(n + 2, {0, 0});
for(int i = 1; i <= n; i++) {
int c = s[i] - 'A' + 1;
hs[i] = hs[i - 1] * base + mp(c, c);
}
}
pair<int, int> getHash(int l, int r) {
if (r > n || l < 0) return {0, 0};
return hs[r] - hs[l - 1] * pw[r - l + 1];
}
bool operator < (Data other) {
int l = 1, r = min(n, other.n), pos = 0;
while(l <= r) {
int mid = (l + r) >> 1;
if (getHash(1, mid) == other.getHash(1, mid)) {
pos = mid;
l = mid + 1;
}
else r = mid - 1;
}
if (pos == min(n, other.n)) return n < other.n;
return s[pos + 1] < other.s[pos + 1];
}
};
int n, q;
int pos[N];
Data a[N];
pair<Data, Data> queries[N];
void read() {
cin >> n >> q;
for(int i = 1; i <= n; i++) {
string s; cin >> s;
a[i].init(s);
}
for(int i = 1; i <= q; i++) {
string s1, s2; cin >> s1 >> s2;
queries[i].fi.init(s1);
queries[i].se.init(s2);
pos[i] = i;
}
}
void sub2() {
for(int iter = 1; iter <= q; iter++) {
int ans = 0;
int n1 = queries[iter].fi.n;
int n2 = queries[iter].se.n;
pair<int, int> val1 = queries[iter].fi.hs[n1];
pair<int, int> val2 = queries[iter].se.hs[n2];
// cerr << val1.fi << ' ' << val1.se << endl;
for(int i = 1; i <= n; i++) {
if (max(n1, n2) <= a[i].n) {
if (a[i].getHash(1, n1) == val1 && a[i].getHash(a[i].n - n2 + 1, a[i].n) == val2) ++ans;
}
}
cout << ans << endl;
}
}
void sub3() {
sort(a + 1, a + n + 1);
sort(pos + 1, pos + q + 1, [&](const int &x, const int &y) {
return queries[x].fi < queries[y].fi;
});
vector<int> ans(q + 1, 0);
for(int iter = 1, l = 1, r = 1; iter <= q; iter++) {
int i = pos[iter];
while(l <= n && a[l] < queries[i].fi) ++l;
int n1 = queries[i].fi.n;
pair<int, int> val1 = queries[i].fi.hs[n1];
int n2 = queries[i].se.n;
pair<int, int> val2 = queries[i].se.hs[n2];
for(int j = l; j <= n; j++) {
if (a[j].getHash(1, n1) == val1) {
if (a[j].getHash(a[j].n - n2 + 1, a[j].n) == val2) ++ans[i];
}
else break;
}
}
for(int i = 1; i <= q; i++) cout << ans[i] << endl;
}
void solve() {
sub3(); return;
if (max(n, q) <= 5000) sub2();
else sub3();
}
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if (fopen(TASK".inp", "r")) {
freopen(TASK".inp", "r", stdin);
freopen(TASK".out", "w", stdout);
}
pw[0] = {1, 1};
for(int i = 1; i < N; i++) pw[i] = pw[i - 1] * base;
int test = 1;
// cin >> test;
while(test--) {
read();
solve();
}
return 0;
}
Compilation message (stderr)
selling_rna.cpp: In function 'int main()':
selling_rna.cpp:132:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
132 | freopen(TASK".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
selling_rna.cpp:133:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
133 | freopen(TASK".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |