Submission #250582

#TimeUsernameProblemLanguageResultExecution timeMemory
250582parsa_mobedSelling RNA Strands (JOI16_selling_rna)C++14
100 / 100
1248 ms666044 KiB
#include <bits/stdc++.h> using namespace std; #define all(v) v.begin(), v.end() const int N = 2e6 + 1, M = 1e5 + 1; int ans[M]; string s[M], p[M], q[M]; vector <int> Q[N], V[N]; void convert(string &s) { for (int i = 0; i < (int)s.size(); i++) { if (s[i] == 'A') s[i] = 0; if (s[i] == 'G') s[i] = 1; if (s[i] == 'C') s[i] = 2; if (s[i] == 'U') s[i] = 3; } return ; } struct Trie { int SGM; Trie(int sgm = 4) : SGM(sgm){} vector <int*> vec = {new int[SGM]{}}; vector <int> cnt = {0}, subT = {0}; inline void addv() {vec.push_back(new int[SGM]{}), cnt.push_back(0), subT.push_back(0); return ;} inline int* operator [] (int i) {return vec[i];} inline int add(const string &s) { int cur = 0; for (int i = 0; i < (int)s.size(); i++) { if (!vec[cur][(int)s[i]]) vec[cur][(int)s[i]] = vec.size(), addv(); subT[cur = vec[cur][(int)s[i]]]++; } cnt[cur]++; return cur; } inline int get(const string &s) { int cur = 0; for (int i = 0; i < (int)s.size(); i++) { if (!vec[cur][(int)s[i]]) return 0; cur = vec[cur][(int)s[i]]; } return cur; } inline int count(const string &s) {return subT[get(s)];} inline void clear() { vec.clear(), vec.shrink_to_fit(); cnt.clear(), cnt.shrink_to_fit(); subT.clear(), subT.shrink_to_fit(); return ; } inline void swap(Trie &T) { vec.swap(T.vec); cnt.swap(T.cnt); subT.swap(T.subT); return ; } } T, tri[N]; void dfs(int v) { int big = 0; for (int i = 0; i < 4; i++) if (T[v][i]) { dfs(T[v][i]); if (V[T[v][i]].size() > V[big].size()) big = T[v][i]; } if (v == 0) return ; if (big == 0) for (int u : V[v]) tri[v].add(s[u]); else V[v].swap(V[big]), tri[v].swap(tri[big]); for (int i = 0; i < 4; i++) if (T[v][i]) { for (int u : V[T[v][i]]) V[v].push_back(u), tri[v].add(s[u]); V[T[v][i]].clear(), V[T[v][i]].shrink_to_fit(); tri[T[v][i]].clear(); } for (int i : Q[v]) ans[i] = tri[v].count(q[i]); } int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); int n, m; cin >> n >> m; for (int i = 0; i < n; i++) cin >> s[i], convert(s[i]), V[T.add(s[i])].push_back(i), reverse(all(s[i])); for (int i = 0; i < m; i++) { cin >> p[i] >> q[i], convert(p[i]), convert(q[i]), reverse(all(q[i]));; int v = T.get(p[i]); if (v == 0) continue ; Q[v].push_back(i); } dfs(0); for (int i = 0; i < m; i++) cout << ans[i] << "\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...