# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
972582 | duckindog | Selling RNA Strands (JOI16_selling_rna) | C++17 | 898 ms | 1048576 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int N = 1'000'000 + 10;
int n, m;
string str[N];
string st[N], ed[N];
int convert(const auto& a) { return a == 'A' ? 0 : a == 'C' ? 1 : a == 'G' ? 2 : 3; }
int nwt[N << 2][4];
bool mk[N << 2];
struct Trie {
int it;
vector<array<int, 4>> nwt;
vector<int> sz;
Trie() : nwt(1), sz(1) {}
void add(string s) {
int g = 0;
for (int i = 1; i <= (int)s.size(); ++i) {
while (nwt.size() <= g) nwt.push_back(array<int, 4>());
int path = convert(s[i - 1]);
if (!nwt[g][path]) nwt[g][path] = ++it;
g = nwt[g][path];
while (sz.size() <= it) sz.push_back(0);
sz[g] += 1;
}
}
int get(string s) {
int g = 0;
for (int i = 1; i <= (int)s.size(); ++i) {
int path = convert(s[i - 1]);
if (!nwt[g][path]) return 0;
g = nwt[g][path];
}
return sz[g];
}
} T[N << 2];
int it;
void mark(string s) {
int g = 0;
for (int i = 1; i <= (int)s.size(); ++i) {
int path = convert(s[i - 1]);
if (!nwt[g][path]) nwt[g][path] = ++it;
g = nwt[g][path];
}
mk[g] = true;
}
void add(string s) {
string rs = string(s.rbegin(), s.rend());
int g = 0;
for (int i = 1; i <= (int)s.size(); ++i) {
int path = convert(s[i - 1]);
if (!nwt[g][path]) nwt[g][path] = ++it;
g = nwt[g][path];
if (mk[g]) T[g].add(rs);
}
}
int get(string s, const auto& e) {
int g = 0;
for (int i = 1; i <= (int)s.size(); ++i) {
int path = convert(s[i - 1]);
g = nwt[g][path];
}
return T[g].get(string(e.rbegin(), e.rend()));
}
int32_t main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> m;
for (int i = 1; i <= n; ++i) cin >> str[i];
for (int i = 1; i <= m; ++i) cin >> st[i] >> ed[i];
for (int i = 1; i <= m; ++i) mark(st[i]);
for (int i = 1; i <= n; ++i) add(str[i]);
for (int i = 1; i <= m; ++i) cout << get(st[i], ed[i]) << "\n";
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |