#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 1e5 + 2;
const int M = 2e6 + 2;
const int inf = 2e9;
int Convert(char x) {
if (x == 'A') return 0;
if (x == 'C') return 1;
if (x == 'G') return 2;
if (x == 'U') return 3;
}
vector<int> a[M];
int b[N];
struct Trie {
vector<int> g[M];
int nxt[M][4], tsz, in[M], out[M];
int Add(string s) {
int node = 0;
for (int i = 0; i < s.size(); i++) {
int y = Convert(s[i]);
if (nxt[node][y] == 0) nxt[node][y] = ++tsz, g[node].push_back(tsz);
node = nxt[node][y];
}
return node;
}
int Get(string s) {
int node = 0;
for (int i = 0; i < s.size(); i++) {
int y = Convert(s[i]);
if (nxt[node][y] == 0) return -1;
node = nxt[node][y];
}
return node;
}
int Compute(int s, int x) {
in[s] = x;
for (int u : g[s]) {
x = Compute(u, x + 1);
}
return out[s] = x;
}
}pre, suf;
vector<pair<int, int>> qq[N];
struct Bit {
int bit[N];
void Add(int x) {
x += 1;
for (; x < N; x += x & (-x)) bit[x]++;
}
int F(int x) {
x += 1; int ans = 0;
for (; x > 0; x -= x & (-x)) ans += bit[x];
return ans;
}
int Get(int l, int r) {
return F(r) - F(l - 1);
}
}f;
int ans[N];
void Dfs(int s) {
for (auto u : qq[s]) {
ans[u.second] -= f.Get(suf.in[u.first], suf.out[u.first]);
}
for (int u : a[s]) f.Add(suf.in[b[u]]);
for (int i = 0; i < 4; i++) {
if (pre.nxt[s][i]) Dfs(pre.nxt[s][i]);
}
for (auto u : qq[s]) {
ans[u.second] += f.Get(suf.in[u.first], suf.out[u.first]);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++) {
string s; cin >> s;
int x = pre.Add(s);
reverse(s.begin(), s.end());
int y = suf.Add(s);
a[x].push_back(i);
b[i] = y;
}
suf.Compute(0, 0);
for (int i = 0; i < m; i++) {
string s, t;
cin >> s >> t;
int x = pre.Get(s);
reverse(t.begin(), t.end());
int y = suf.Get(t);
if (min(x, y) > -1) qq[x].push_back({y, i});
}
Dfs(0);
for (int i = 0; i < m; i++) cout << ans[i] << en;
return 0;
}
Compilation message
selling_rna.cpp: In member function 'int Trie::Add(std::string)':
selling_rna.cpp:25:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
25 | for (int i = 0; i < s.size(); i++) {
| ~~^~~~~~~~~~
selling_rna.cpp: In member function 'int Trie::Get(std::string)':
selling_rna.cpp:34:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
34 | for (int i = 0; i < s.size(); i++) {
| ~~^~~~~~~~~~
selling_rna.cpp: In function 'int Convert(char)':
selling_rna.cpp:17:1: warning: control reaches end of non-void function [-Wreturn-type]
17 | }
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
66 ms |
143588 KB |
Output is correct |
2 |
Correct |
67 ms |
143572 KB |
Output is correct |
3 |
Correct |
75 ms |
143564 KB |
Output is correct |
4 |
Correct |
66 ms |
143652 KB |
Output is correct |
5 |
Correct |
66 ms |
143656 KB |
Output is correct |
6 |
Correct |
70 ms |
143648 KB |
Output is correct |
7 |
Correct |
75 ms |
143648 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
230 ms |
256092 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
87 ms |
145532 KB |
Output is correct |
2 |
Correct |
90 ms |
145300 KB |
Output is correct |
3 |
Correct |
84 ms |
145380 KB |
Output is correct |
4 |
Correct |
87 ms |
144892 KB |
Output is correct |
5 |
Correct |
80 ms |
145080 KB |
Output is correct |
6 |
Correct |
87 ms |
145288 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
66 ms |
143588 KB |
Output is correct |
2 |
Correct |
67 ms |
143572 KB |
Output is correct |
3 |
Correct |
75 ms |
143564 KB |
Output is correct |
4 |
Correct |
66 ms |
143652 KB |
Output is correct |
5 |
Correct |
66 ms |
143656 KB |
Output is correct |
6 |
Correct |
70 ms |
143648 KB |
Output is correct |
7 |
Correct |
75 ms |
143648 KB |
Output is correct |
8 |
Incorrect |
230 ms |
256092 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |