#include <bits/stdc++.h>
using namespace std;
using ll = long long;
// #define int long long
#define pii pair<ll, ll>
#define fi first
#define se second
const ll N = 2e5 + 5, inf = 1e18, mod = 1e9 + 7, block = 320, lim = 1 << 16;
int n, m;
string a[N], b[N], c[N];
int L[2000005], R[2000005];
int child[2][2000005][26], cnt;
vector <int> pre[2000005];
void add(int type, string s, int pos) {
int u = 0;
if (type) reverse(s.begin(), s.end());
for (int i = 0; i < s.size(); i++) {
int k = s[i] - '0';
if (!child[type][u][k]) child[type][u][k] = ++cnt;
u = child[type][u][k];
if (!type) {
if (!L[u]) L[u] = pos;
R[u] = pos;
}
else {
pre[u].push_back(pos);
}
}
}
int get0 (string s) {
int u = 0;
for (int i = 0; i < s.size(); i++) {
int k = s[i] - '0';
if (!child[0][u][k]) return 0;
u = child[0][u][k];
}
return u;
}
vector <int> get1 (string s) {
int u = 0;
reverse(s.begin(), s.end());
for (int i = 0; i < s.size(); i++) {
int k = s[i] - '0';
if (!child[1][u][k]) return vector <int> (1, 0);
u = child[1][u][k];
}
return pre[u];
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
if (fopen(".inp", "r")) {
freopen(".inp", "r", stdin);
freopen(".out", "w", stdout);
}
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 1; i <= m; i++) {
cin >> b[i] >> c[i];
}
sort(a + 1, a + n + 1);
for (int i = 1; i <= n; i++) {
add(0, a[i], i);
add(1, a[i], i);
}
for (int i = 1; i <= m; i++) {
auto val1 = get0(b[i]); // doan [L, R] tien to
auto val2 = get1(c[i]); // vector chua cac con thoa man hau to
if (!val1 || val2[0] == 0) {
cout << 0 << '\n';
continue;
}
auto it1 = lower_bound(val2.begin(), val2.end(), L[val1]) - val2.begin();
auto it2 = upper_bound(val2.begin(), val2.end(), R[val1]) - val2.begin() - 1;
cout << it2 - it1 + 1 << '\n';
}
return 0;
}
Compilation message (stderr)
selling_rna.cpp: In function 'int main()':
selling_rna.cpp:60:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
60 | freopen(".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~
selling_rna.cpp:61:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
61 | freopen(".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... |