#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef string str;
typedef double db;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pi> vpi;
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define FR(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define RF(i,a) ROF(i,0,a)
#define trav(a,x) for (auto& a: x)
const int MAXN = 5;
const int MAXNODES = 15;
struct Node {
int to[4] = {-1, -1, -1, -1};
int leaf = -1;
int cnt = 0;
};
vi prefix[MAXN];
vi suffix[MAXN];
int cur[MAXN];
int ans[MAXN];
vector<Node> nodes;
vi ss[MAXN];
vi ssrev[MAXN];
struct Trie {
int root = -1;
vector<int> leaves;
int sz = 0;
Trie() {
root = nodes.size();
nodes.push_back({});
}
void add(int i, vi& a) {
sz += a.size();
leaves.push_back(i);
iter(root, 0, i, a);
}
void iter(int j, int cur, int i, vi& s) {
nodes[j].cnt++;
if (cur == s.size()-1) {
nodes[j].leaf = i;
return;
}
if (nodes[j].to[s[cur]] == -1) {
nodes[j].to[s[cur]] = nodes.size();
nodes.push_back({});
}
iter(nodes[j].to[s[cur]], cur+1, i, s);
}
int cnt(vi& a, int u, int cur = 0) {
if (u == -1) {
return 0;
}
if (cur == a.size()) {
return nodes[u].cnt;
}
cnt(a, nodes[u].to[a[cur]], cur+1);
}
};
Trie start;
Trie ts[MAXN];
int tr[MAXNODES];
int merge(int u, int j) {
if (ts[u].sz < ts[j].sz) {
swap(u, j);
}
trav(v, ts[j].leaves) {
ts[u].add(v, ssrev[v]);
}
return u;
}
void dfs(int u, vi& prefixes) {
if (nodes[u].leaf != -1) {
tr[u] = nodes[u].leaf;
}
vi send[5];
trav(a, prefixes) {
if (cur[a] == prefix[a].size()) {
send[4].push_back(a);
}
else {
send[prefix[a][cur[a]++]].push_back(a);
}
}
FR(i, 4) {
if (nodes[u].to[i] != -1) {
dfs(nodes[u].to[i], send[i]);
if (tr[u] == -1) {
tr[u] = tr[nodes[u].to[i]];
}
else {
tr[u] = merge(tr[u], tr[nodes[u].to[i]]);
}
}
}
trav(a, send[4]) {
ans[a] = ts[tr[u]].cnt(suffix[a], ts[tr[u]].root);
}
}
int conv[300];
int main() {
int N, M;
cin >> N >> M;
conv['A'] = 0;
conv['G'] = 1;
conv['C'] = 2;
conv['U'] = 3;
FR(i, MAXNODES) {
tr[i] = -1;
}
FR(i, N) {
string t;
cin >> t;
FR(j, t.size()) {
ss[i].push_back(conv[t[j]]);
ssrev[i].push_back(conv[t[j]]);
}
reverse(ssrev[i].begin(), ssrev[i].end());
start.add(i, ss[i]);
ts[i].add(i, ssrev[i]);
}
vi ori;
FR(i, M) {
string pre, suff;
cin >> pre >> suff;
FR(j, pre.size()) {
prefix[i].push_back(conv[pre[j]]);
}
FR(j, suff.size()) {
suffix[i].push_back(conv[suff[j]]);
}
reverse(suffix[i].begin(), suffix[i].end());
ori.push_back(i);
}
dfs(start.root, ori);
FR(i, M) {
cout << ans[i] << '\n';
}
}
Compilation message
selling_rna.cpp: In member function 'void Trie::iter(int, int, int, vi&)':
selling_rna.cpp:51:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
51 | if (cur == s.size()-1) {
| ~~~~^~~~~~~~~~~~~
selling_rna.cpp: In member function 'int Trie::cnt(vi&, int, int)':
selling_rna.cpp:65:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
65 | if (cur == a.size()) {
| ~~~~^~~~~~~~~~~
selling_rna.cpp: In function 'void dfs(int, vi&)':
selling_rna.cpp:89:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
89 | if (cur[a] == prefix[a].size()) {
| ~~~~~~~^~~~~~~~~~~~~~~~~~~
selling_rna.cpp: In function 'int main()':
selling_rna.cpp:17:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
17 | #define FOR(i,a,b) for (int i = (a); i < (b); ++i)
| ^
selling_rna.cpp:18:17: note: in expansion of macro 'FOR'
18 | #define FR(i,a) FOR(i,0,a)
| ^~~
selling_rna.cpp:126:9: note: in expansion of macro 'FR'
126 | FR(j, t.size()) {
| ^~
selling_rna.cpp:127:38: warning: array subscript has type 'char' [-Wchar-subscripts]
127 | ss[i].push_back(conv[t[j]]);
| ^
selling_rna.cpp:128:41: warning: array subscript has type 'char' [-Wchar-subscripts]
128 | ssrev[i].push_back(conv[t[j]]);
| ^
selling_rna.cpp:17:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
17 | #define FOR(i,a,b) for (int i = (a); i < (b); ++i)
| ^
selling_rna.cpp:18:17: note: in expansion of macro 'FOR'
18 | #define FR(i,a) FOR(i,0,a)
| ^~~
selling_rna.cpp:138:9: note: in expansion of macro 'FR'
138 | FR(j, pre.size()) {
| ^~
selling_rna.cpp:139:44: warning: array subscript has type 'char' [-Wchar-subscripts]
139 | prefix[i].push_back(conv[pre[j]]);
| ^
selling_rna.cpp:17:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
17 | #define FOR(i,a,b) for (int i = (a); i < (b); ++i)
| ^
selling_rna.cpp:18:17: note: in expansion of macro 'FOR'
18 | #define FR(i,a) FOR(i,0,a)
| ^~~
selling_rna.cpp:141:9: note: in expansion of macro 'FR'
141 | FR(j, suff.size()) {
| ^~
selling_rna.cpp:142:45: warning: array subscript has type 'char' [-Wchar-subscripts]
142 | suffix[i].push_back(conv[suff[j]]);
| ^
selling_rna.cpp: In member function 'int Trie::cnt(vi&, int, int)':
selling_rna.cpp:68:12: warning: control reaches end of non-void function [-Wreturn-type]
68 | cnt(a, nodes[u].to[a[cur]], cur+1);
| ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
2 ms |
620 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
5 ms |
2728 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
492 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
2 ms |
620 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |