#include <bits/stdc++.h>
using namespace std;
struct Prefix{
Prefix* nxt[4];
int Min = numeric_limits<int>::max(), Max = numeric_limits<int>::min();
};
struct Suffix{
Suffix* nxt[4];
vector<int> indices;
};
static inline int idx(const char& ch){
if (ch == 'A') return 0;
if (ch == 'C') return 1;
if (ch == 'G') return 2;
if (ch == 'U') return 3;
return -1;
}
void add_str(Prefix* root, const string &s, int idc){
Prefix* tmp = root;
tmp->Min = min(tmp->Min, idc);
tmp->Max = max(tmp->Max, idc);
for (const char &ch : s){
assert(idx(ch) >= 0);
if (tmp->nxt[idx(ch)] == nullptr){
tmp->nxt[idx(ch)] = new Prefix;
}
tmp = tmp->nxt[idx(ch)];
tmp->Min = min(tmp->Min, idc);
tmp->Max = max(tmp->Max, idc);
}
}
void add_str(Suffix* root, const string &s, int idc){
Suffix* tmp = root;
tmp->indices.push_back(idc);
for (int i = s.size() - 1; i >= 0; i--){
const char &ch = s[i];
assert(idx(ch) >= 0);
if (tmp->nxt[idx(ch)] == nullptr){
tmp->nxt[idx(ch)] = new Suffix;
}
tmp = tmp->nxt[idx(ch)];
tmp->indices.push_back(idc);
}
}
pair<int, int> find_str(Prefix* root, const string &s){
Prefix* tmp = root;
for (const char &ch : s){
assert(idx(ch) >= 0);
if (tmp->nxt[idx(ch)] == nullptr) return {-1, -1};
tmp = tmp->nxt[idx(ch)];
}
return {tmp->Min, tmp->Max};
}
vector<int> find_str(Suffix* root, const string &s){
Suffix* tmp = root;
for (int i = s.size() - 1; i >= 0; i--){
const char &ch = s[i];
assert(idx(ch) >= 0);
if (tmp->nxt[idx(ch)] == nullptr) return {};
tmp = tmp->nxt[idx(ch)];
}
return tmp->indices;
}
int main() {
Suffix *triesuf = new Suffix;
Prefix *triepref = new Prefix;
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++){
string s;
cin >> s;
add_str(triesuf, s, i);
add_str(triepref, s, i);
}
for (int i = 0; i < m; i++){
string p, q;
cin >> p >> q;
vector<int> a = find_str(triesuf, q);
pair<int, int> lol = find_str(triepref, p);
if (a.empty()){
cout << 0 << endl;
continue;
}
if (lol.first == -1){
cout << 0 << endl;
continue;
}
auto it = lower_bound(a.begin(), a.end(), lol.first);
auto jt = upper_bound(a.begin(), a.end(), lol.second);
int res = jt - it;
cout << res << endl;
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
340 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
724 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
166 ms |
1800 KB |
Output is correct |
2 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
340 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |