Submission #784850

#TimeUsernameProblemLanguageResultExecution timeMemory
784850vjudge1Selling RNA Strands (JOI16_selling_rna)C++17
0 / 100
166 ms1800 KiB
#include <bits/stdc++.h> using namespace std; struct Prefix{ Prefix* nxt[4]; int Min = numeric_limits<int>::max(), Max = numeric_limits<int>::min(); }; struct Suffix{ Suffix* nxt[4]; vector<int> indices; }; static inline int idx(const char& ch){ if (ch == 'A') return 0; if (ch == 'C') return 1; if (ch == 'G') return 2; if (ch == 'U') return 3; return -1; } void add_str(Prefix* root, const string &s, int idc){ Prefix* tmp = root; tmp->Min = min(tmp->Min, idc); tmp->Max = max(tmp->Max, idc); for (const char &ch : s){ assert(idx(ch) >= 0); if (tmp->nxt[idx(ch)] == nullptr){ tmp->nxt[idx(ch)] = new Prefix; } tmp = tmp->nxt[idx(ch)]; tmp->Min = min(tmp->Min, idc); tmp->Max = max(tmp->Max, idc); } } void add_str(Suffix* root, const string &s, int idc){ Suffix* tmp = root; tmp->indices.push_back(idc); for (int i = s.size() - 1; i >= 0; i--){ const char &ch = s[i]; assert(idx(ch) >= 0); if (tmp->nxt[idx(ch)] == nullptr){ tmp->nxt[idx(ch)] = new Suffix; } tmp = tmp->nxt[idx(ch)]; tmp->indices.push_back(idc); } } pair<int, int> find_str(Prefix* root, const string &s){ Prefix* tmp = root; for (const char &ch : s){ assert(idx(ch) >= 0); if (tmp->nxt[idx(ch)] == nullptr) return {-1, -1}; tmp = tmp->nxt[idx(ch)]; } return {tmp->Min, tmp->Max}; } vector<int> find_str(Suffix* root, const string &s){ Suffix* tmp = root; for (int i = s.size() - 1; i >= 0; i--){ const char &ch = s[i]; assert(idx(ch) >= 0); if (tmp->nxt[idx(ch)] == nullptr) return {}; tmp = tmp->nxt[idx(ch)]; } return tmp->indices; } int main() { Suffix *triesuf = new Suffix; Prefix *triepref = new Prefix; int n, m; cin >> n >> m; for (int i = 0; i < n; i++){ string s; cin >> s; add_str(triesuf, s, i); add_str(triepref, s, i); } for (int i = 0; i < m; i++){ string p, q; cin >> p >> q; vector<int> a = find_str(triesuf, q); pair<int, int> lol = find_str(triepref, p); if (a.empty()){ cout << 0 << endl; continue; } if (lol.first == -1){ cout << 0 << endl; continue; } auto it = lower_bound(a.begin(), a.end(), lol.first); auto jt = upper_bound(a.begin(), a.end(), lol.second); int res = jt - it; cout << res << endl; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...