Submission #497642

#TimeUsernameProblemLanguageResultExecution timeMemory
497642khoabrightSelling RNA Strands (JOI16_selling_rna)C++17
0 / 100
200 ms194300 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long #define pii pair<int, int> #define ff first #define ss second #define pb push_back #define rep(i, a, b) for (int i = (int)a; i <= (int)b; ++i) #define rep1(i, a, b) for (int i = (int)a; i >= (int)b; --i) #define all(x) x.begin(), x.end() #define all1(x) x.begin() + 1, x.end() const int N = 1e5 + 5; const int inf = 1e9; int n, m; string s[N]; struct node1 { node1* child[4] = {}; int left = inf, right = -inf; }; struct node2 { node2* child[4] = {}; vector<int> pos; }; node1* root1 = new node1; node2* root2 = new node2; void add1(string ss, int idx) { node1* cur = root1; for (char c : ss) { int x = c - 'a'; if (!cur -> child[x]) cur -> child[x] = new node1; cur = cur -> child[x]; cur -> left = min(cur -> left, idx); cur -> right = max(cur -> right, idx); } } void add2(string ss, int idx) { node2* cur = root2; for (char c : ss) { int x = c - 'a'; if (!cur -> child[x]) cur -> child[x] = new node2; cur = cur -> child[x]; cur -> pos.pb(idx); } } pii find1(string s) { node1* cur = root1; for (char c : s) { int x = c - 'a'; if (!cur -> child[x]) return {-1, -1}; cur = cur -> child[x]; } return {cur -> left, cur -> right}; } int find2(string s, pii range) { if (range.ff == -1) return 0; vector<int> res; node2* cur = root2; for (char c : s) { int x = c - 'a'; if (!cur -> child[x]) return 0; cur = cur -> child[x]; } auto l = lower_bound(all(cur -> pos), range.ff); auto r = upper_bound(all(cur -> pos), range.ss); return r - l; } bool cmp(string &a, string &b) { int sz = min(a.size(), b.size()); rep(i, 0, sz - 1) { if (a[i] != b[i]) return a[i] < b[i]; } return a.size() < b.size(); } void convert(string &t) { rep(i, 0, t.size() - 1) { if (t[i] == 'A') t[i] = 'a'; if (t[i] == 'C') t[i] = 'b'; if (t[i] == 'G') t[i] = 'c'; if (t[i] == 'U') t[i] = 'd'; } } void init() { cin >> n >> m; rep(i, 1, n) { cin >> s[i]; convert(s[i]); } sort(s + 1, s + n + 1, cmp); //rep(i, 1, n) cout << s[i] << '\n'; rep(i, 1, n) add1(s[i], i); rep(i, 1, n) reverse(all(s[i])); rep(i, 1, n) add2(s[i], i); } void solve() { rep(_m, 1, m) { string pref, suf; cin >> pref >> suf; convert(pref); convert(suf); pii tmp = find1(pref); //cout<<"tmp="<<tmp.ff<<' '<<tmp.ss<<'\n'; int ans = find2(suf, tmp); cout << ans << '\n'; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); init(); solve(); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...