#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <bitset>
#include <cstring>
#include <cassert>
using namespace std;
const int MAXN = 5000;
using VI = vector<int>;
using BS = bitset<MAXN>;
//using BS = vector<bool>;
struct Trie {
struct TrieNode {
int child[4];
VI ends_here;
TrieNode() {
memset(child, -1, sizeof(child));
}
};
vector<TrieNode> nodes;
static const int root = 0; // root is at index 0
Trie() {
// nodes.reserve(20000);
nodes.push_back(TrieNode()); // 1st node is root
}
void insert(const string& S, int id) {
int cur = root;
for (char c : S) {
int k = c - '0';
int nxt = nodes[cur].child[k];
if (nxt < 0) {
nxt = nodes.size();
nodes[cur].child[k] = nxt;
nodes.push_back(TrieNode());
}
cur = nxt;
}
nodes[cur].ends_here.push_back(id);
}
void dfs(BS& bs, int cur) {
for (int id : nodes[cur].ends_here)
bs[id] = true;
for (int k = 0; k < 4; ++k) {
int nxt = nodes[cur].child[k];
if (nxt >= 0)
dfs(bs, nxt);
}
}
BS find(const string& S) {
int cur = root;
for (char c : S) {
int k = c - '0';
int nxt = nodes[cur].child[k];
if (nxt < 0)
return BS();
cur = nxt;
}
BS ret;
dfs(ret, cur);
return ret;
}
};
const string valid_chars = "ACGU";
void transform(string& S) {
for (int i = 0; i < (int) S.size(); ++i)
S[i] = '0' + valid_chars.find( S[i] );
}
int main(int argc, char* argv[]) {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
Trie trie_fwd, trie_rev;
int N, M;
cin >> N >> M;
if (N > 5000) return 0;
for (int i = 0; i < N; ++i) {
string chain;
cin >> chain;
transform(chain);
trie_fwd.insert(chain, i);
reverse(chain.begin(), chain.end());
trie_rev.insert(chain, i);
}
for (int j = 0; j < M; ++j) {
string start, end;
cin >> start >> end;
transform(start);
transform(end);
reverse(end.begin(), end.end());
BS sf = trie_fwd.find(start);
BS ef = trie_rev.find(end);
int res = (sf & ef).count();
/*
int res = 0;
for (int i = 0; i < N; ++i)
if (sf[i] and ef[i])
++res;
*/
cout << res << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
316 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1603 ms |
84032 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
316 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Execution timed out |
1603 ms |
84032 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |