#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;
#define SIZE 2000000
int index(char c) {
if (c == 'A') return 0;
else if (c == 'C') return 1;
else if (c == 'G') return 2;
else return 3;
}
int trie[SIZE+1][4],num = 0;
int leaf[SIZE+1];
vector<pair<string,int> > queries[SIZE+1];
int ans[100000];
int trie2[SIZE+200000][4],sub[SIZE+200000],num2 = 0,p[SIZE+1];
int merge(int a,int b) {
int i;
sub[b] += sub[a];
for (i = 0; i < 4; i++) {
if (trie2[a][i] != 0) {
if (trie2[b][i] == 0) trie2[b][i] = ++num2;
merge(trie2[a][i],trie2[b][i]);
}
}
return 0;
}
vi path;
int add(int a,int n) {
int i;
int c = a;
for (i = path.size()-1; i >= 0; i--) {
sub[c] += n;
if (trie2[c][path[i]] == 0) trie2[c][path[i]] = ++num2;
c = trie2[c][path[i]];
}
sub[c] += n;
return 0;
}
int doDFS(int u) {
int i;
int h = -1;
for (i = 0; i < 4; i++) {
if (trie[u][i] != 0) {
path.pb(i);
doDFS(trie[u][i]);
if ((h == -1) || (sub[p[trie[u][i]]] > sub[h])) h = p[trie[u][i]];
path.pop_back();
}
}
if (h == -1) h = ++num2;
if (leaf[u] > 0) add(h,leaf[u]);
for (i = 0; i < 4; i++) {
if ((trie[u][i] != 0) && (p[trie[u][i]] != h)) merge(p[trie[u][i]],h);
}
int j;
p[u] = h;
for (i = 0; i < queries[u].size(); i++) {
int c = h;
for (j = 0; j < queries[u][i].first.size(); j++) {
int k = index(queries[u][i].first[j]);
if (trie2[c][k] == 0) break;
else c = trie2[c][k];
}
if (j >= queries[u][i].first.size()) ans[queries[u][i].second] = sub[c];
}
return 0;
}
int main() {
cin.sync_with_stdio(0);
cout.sync_with_stdio(0);
cin.tie(0);
int i,j;
int N,M;
string s1,s2;
cin >> N >> M;
for (i = 0; i < N; i++) {
cin >> s1;
int c = 0;
for (j = 0; j < s1.size(); j++) {
if (trie[c][index(s1[j])] == 0) trie[c][index(s1[j])] = ++num;
c = trie[c][index(s1[j])];
}
leaf[c]++;
}
for (i = 0; i < M; i++) {
cin >> s1 >> s2;
int c = 0;
for (j = 0; j < s1.size(); j++) {
if (trie[c][index(s1[j])] == 0) break;
c = trie[c][index(s1[j])];
}
if (j >= s1.size()) {
reverse(s2.begin(),s2.end());
queries[c].pb(mp(s2,i));
}
}
doDFS(0);
for (i = 0; i < M; i++) cout << ans[i] << endl;
return 0;
}
Compilation message
selling_rna.cpp: In function 'int doDFS(int)':
selling_rna.cpp:62:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i = 0; i < queries[u].size(); i++) {
~~^~~~~~~~~~~~~~~~~~~
selling_rna.cpp:64:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (j = 0; j < queries[u][i].first.size(); j++) {
~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
selling_rna.cpp:69:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (j >= queries[u][i].first.size()) ans[queries[u][i].second] = sub[c];
~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
selling_rna.cpp: In function 'int main()':
selling_rna.cpp:85:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (j = 0; j < s1.size(); j++) {
~~^~~~~~~~~~~
selling_rna.cpp:94:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (j = 0; j < s1.size(); j++) {
~~^~~~~~~~~~~
selling_rna.cpp:98:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (j >= s1.size()) {
~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
47 ms |
47352 KB |
Output is correct |
2 |
Correct |
44 ms |
47552 KB |
Output is correct |
3 |
Correct |
48 ms |
47552 KB |
Output is correct |
4 |
Correct |
41 ms |
47552 KB |
Output is correct |
5 |
Correct |
41 ms |
47552 KB |
Output is correct |
6 |
Correct |
42 ms |
47552 KB |
Output is correct |
7 |
Correct |
42 ms |
47552 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1612 ms |
1049600 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
133 ms |
1049600 KB |
Memory limit exceeded: We have a known bug that the memory usage is measured incorrectly (possibly because of Meltdown/Spectre patch), so your solution may be correct. Please submit again. Sorry for the inconvenience. |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
47 ms |
47352 KB |
Output is correct |
2 |
Correct |
44 ms |
47552 KB |
Output is correct |
3 |
Correct |
48 ms |
47552 KB |
Output is correct |
4 |
Correct |
41 ms |
47552 KB |
Output is correct |
5 |
Correct |
41 ms |
47552 KB |
Output is correct |
6 |
Correct |
42 ms |
47552 KB |
Output is correct |
7 |
Correct |
42 ms |
47552 KB |
Output is correct |
8 |
Execution timed out |
1612 ms |
1049600 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |