Submission #396534

#TimeUsernameProblemLanguageResultExecution timeMemory
396534SundavarSelling RNA Strands (JOI16_selling_rna)C++14
100 / 100
561 ms344368 KiB
#include <bits/stdc++.h> using namespace std; int get(char c){ if(c == 'A') return 1; if(c == 'G') return 2; if(c == 'C') return 3; return 0; } struct node{ vector<int> to; vector<int> veg, pont; int cnt = 0; node(){ to.resize(4, -1); } }; vector<node> trie(1), rev(1); int add(vector<node>& t, string s, int id, bool normal = true){ int poz = 0; for(char& c: s){ int k = get(c); if(t[poz].to[k] == -1){ t[poz].to[k] = t.size(); t.push_back(node()); } t[poz = t[poz].to[k]].cnt++; } if(normal) t[poz].veg.push_back(id); else t[poz].pont.push_back(id); return poz; } vector<int> poz; vector<int> ans; vector<string> p,q, revs; void walk(int curr){ for(int& a : trie[curr].pont) ans[a] -= rev[poz[a]].cnt; for(int& a : trie[curr].veg) add(rev, revs[a], a); for(int& to : trie[curr].to){ if(to == -1) continue; walk(to); } for(int& a : trie[curr].pont) ans[a] += rev[poz[a]].cnt; } int main(){ cin.sync_with_stdio(false); cout.sync_with_stdio(false); int n,m; cin>>n>>m; vector<string> s(n); for(int i = 0; i < n; i++){ cin>>s[i]; add(trie, s[i], i); revs.push_back(s[i]), reverse(revs[i].begin(), revs[i].end()); } poz.resize(m), ans.resize(m); p.resize(m), q.resize(m); for(int i = 0; i < m; i++){ cin>>p[i]>>q[i]; reverse(q[i].begin(), q[i].end()); add(trie, p[i], i, false); poz[i] = add(rev, q[i], i, false); } walk(0); for(int& a : ans) cout<<a<<"\n"; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...