Submission #1032863

#TimeUsernameProblemLanguageResultExecution timeMemory
1032863chautanphatSelling RNA Strands (JOI16_selling_rna)C++14
100 / 100
462 ms601172 KiB
#include <bits/stdc++.h> using namespace std; struct Pre { struct node { int l, r; node* child[26]; node () { l = 1e9, r = 0; for (int i = 0; i < 26; i++) child[i] = NULL; } } *root; Pre () { root = new node(); } void add(string s, int idx) { node* cur = root; for (int i = 0; i < (int)s.size(); i++) { int id = s[i]-'A'; if (cur->child[id] == NULL) cur->child[id] = new node(); cur = cur->child[id]; cur->l = min(cur->l, idx), cur->r = max(cur->r, idx); } } pair<int, int> get(string s) { node* cur = root; for (int i = 0; i < (int)s.size(); i++) { int id = s[i]-'A'; if (cur->child[id] == NULL) return {-1, -1}; cur = cur->child[id]; } return {cur->l, cur->r}; } } pre; struct Suf { struct node { vector<int> v; node* child[26]; node () { for (int i = 0; i < 26; i++) child[i] = NULL; } } *root; Suf () { root = new node(); } void add(string s, int idx) { reverse(s.begin(), s.end()); node* cur = root; for (int i = 0; i < (int)s.size(); i++) { int id = s[i]-'A'; if (cur->child[id] == NULL) cur->child[id] = new node(); cur = cur->child[id]; cur->v.push_back(idx); } } void srt(node* cur = NULL) { if (cur == NULL) cur = root; sort(cur->v.begin(), cur->v.end()); for (int i = 0; i < 26; i++) if (cur->child[i] != NULL) srt(cur->child[i]); } int get(string s, int l, int r) { reverse(s.begin(), s.end()); node* cur = root; for (int i = 0; i < (int)s.size(); i++) { int id = s[i]-'A'; if (cur->child[id] == NULL) return 0; cur = cur->child[id]; } int L = lower_bound(cur->v.begin(), cur->v.end(), l)-cur->v.begin(); int R = upper_bound(cur->v.begin(), cur->v.end(), r)-cur->v.begin(); return R-L; } } suf; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; vector<string> s(n); for (int i = 0; i < n; i++) cin >> s[i]; sort(s.begin(), s.end()); for (int i = 0; i < n; i++) pre.add(s[i], i), suf.add(s[i], i); suf.srt(); while (m--) { string p, q; cin >> p >> q; pair<int, int> pr = pre.get(p); if (pr.first == -1) cout << "0\n"; else cout << suf.get(q, pr.first, pr.second) << '\n'; } 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...