#include "bits/stdc++.h"
using namespace std;
const int N = 2e5+10, p = 37, MOD = 1e9+7;
string s[N];
int n, m, P[N];
bool check(const string &a, const string &b) { // a >= b ? true : false
int mini = min((int)a.size(), (int)b.size());
for(int i = 0; i < mini; ++i) {
if(a[i] != b[i]) {
return a[i] > b[i];
}
}
return mini == (int)b.size();
}
bool is_prefix(const string &a, const string &b) {
if((int)a.size() > (int)b.size()) {
return false;
}
for(int i = 0; i < (int)a.size(); ++i) {
if(a[i] != b[i]) {
return false;
}
}
return true;
}
int find_first(const string &p) {
int l = 1, r = n, pos = n+1;
while(r >= l) {
int m = l + r >> 1;
if(check(s[m], p)) {
r = m - 1;
pos = m;
} else {
l = m + 1;
}
}
return pos;
}
int find_last(int start, const string &p) {
int l = start, r = n, pos = 0;
while(r >= l) {
int m = l + r >> 1;
if(is_prefix(p, s[m])) {
pos = m;
l = m + 1;
} else {
r = m - 1;
}
}
return pos;
}
int count(const vector<int> &v, int x) {
if(v.empty()) return 0;
int l = 0, r = (int)v.size()-1, pos = -1;
while(r >= l) {
int m = l + r >> 1;
if(v[m] <= x) {
pos = m;
l = m + 1;
} else {
r = m - 1;
}
}
return pos+1;
}
int count(const vector<int> &v, int L, int R) {
return count(v, R) - count(v, L-1);
}
int id[256];
map<int, vector<int>> ids;
int main() {
P[0] = 1;
for(int i = 1; i < N; ++i) {
P[i] = (long long)P[i - 1] * p % MOD;
}
id['A'] = 1;
id['U'] = 2;
id['G'] = 3;
id['C'] = 4;
ios::sync_with_stdio(0); cin.tie(0);
cin >> n >> m;
for(int i = 1; i <= n; ++i) {
cin >> s[i];
}
sort(s+1, s+n+1);
for(int i = 1; i <= n; ++i) {
int hash = 0;
for(int x = (int)s[i].size() - 1; x >= 0; --x) {
hash = ((long long)hash * p + id[s[i][x]]) % MOD;
ids[hash].push_back(i);
}
}
for(int i = 1; i <= m; ++i) {
string p, q; cin >> p >> q;
int L = n+1, R = 0;
L = find_first(p);
if(!is_prefix(p, s[L])) {
cout << "0\n";
continue;
}
R = find_last(L, p);
assert(is_prefix(p, s[R]));
int hash = 0;
for(int x = 0; x < (int)q.size(); ++x) {
hash = (hash + (long long)P[x] * id[q[x]]) % MOD;
}
if(L > R) {
cout << "0\n";
continue;
}
cout << count(ids[hash], L, R) << '\n';
}
}
Compilation message
selling_rna.cpp: In function 'int find_first(const string&)':
selling_rna.cpp:29:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
29 | int m = l + r >> 1;
| ~~^~~
selling_rna.cpp: In function 'int find_last(int, const string&)':
selling_rna.cpp:42:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
42 | int m = l + r >> 1;
| ~~^~~
selling_rna.cpp: In function 'int count(const std::vector<int>&, int)':
selling_rna.cpp:56:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
56 | int m = l + r >> 1;
| ~~^~~
selling_rna.cpp: In function 'int main()':
selling_rna.cpp:89:44: warning: array subscript has type 'char' [-Wchar-subscripts]
89 | hash = ((long long)hash * p + id[s[i][x]]) % MOD;
| ^
selling_rna.cpp:105:44: warning: array subscript has type 'char' [-Wchar-subscripts]
105 | hash = (hash + (long long)P[x] * id[q[x]]) % MOD;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
7372 KB |
Output is correct |
2 |
Correct |
6 ms |
7368 KB |
Output is correct |
3 |
Correct |
6 ms |
7340 KB |
Output is correct |
4 |
Correct |
6 ms |
7376 KB |
Output is correct |
5 |
Correct |
7 ms |
7272 KB |
Output is correct |
6 |
Correct |
6 ms |
7384 KB |
Output is correct |
7 |
Correct |
7 ms |
7376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1600 ms |
163472 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
41 ms |
8652 KB |
Output is correct |
2 |
Correct |
37 ms |
8788 KB |
Output is correct |
3 |
Correct |
42 ms |
8636 KB |
Output is correct |
4 |
Correct |
32 ms |
8388 KB |
Output is correct |
5 |
Correct |
37 ms |
8396 KB |
Output is correct |
6 |
Correct |
45 ms |
8500 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
7372 KB |
Output is correct |
2 |
Correct |
6 ms |
7368 KB |
Output is correct |
3 |
Correct |
6 ms |
7340 KB |
Output is correct |
4 |
Correct |
6 ms |
7376 KB |
Output is correct |
5 |
Correct |
7 ms |
7272 KB |
Output is correct |
6 |
Correct |
6 ms |
7384 KB |
Output is correct |
7 |
Correct |
7 ms |
7376 KB |
Output is correct |
8 |
Execution timed out |
1600 ms |
163472 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |