# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
87295 | 2018-11-30T07:58:53 Z | tieunhi | Selling RNA Strands (JOI16_selling_rna) | C++14 | 534 ms | 258192 KB |
#include <bits/stdc++.h> #define pii pair<int, int> #define mp make_pair #define F first #define S second #define PB push_back #define FOR(i, u, v) for (int i = u; i <= v; i++) #define FORD(i, v, u) for (int i = v; i >= u; i--) #define ll long long #define mid (r + l)/2 #define N 100005 #define LN 19 using namespace std; int n, numQ, Id[N], tt; string S[N]; string alphabet = "AGCU"; int convertChar(char c) {return alphabet.find(c);} struct TrieNode { TrieNode *child[4]; int tin, tout; vector<int> all; TrieNode() { FOR(i, 0, 3) child[i] = NULL; tin = tout = 0; all.resize(0); } }; TrieNode T, T_rev; void add(string s, int id) { TrieNode *p = &T; FOR(i, 0, s.size()-1) { int c = convertChar(s[i]); if (p->child[c] == NULL) p->child[c] = new TrieNode(); p = p->child[c]; } p->all.PB(id); } void add_rev(string s, int id) { TrieNode *p = &T_rev; FOR(i, 0, s.size()-1) { int c = convertChar(s[i]); if (p->child[c] == NULL) p->child[c] = new TrieNode(); p = p->child[c]; p->all.PB(Id[id]); } } void DFS(TrieNode *p) { p->tin = ++tt; if (p->all.size()) { for (auto x : p->all) Id[x] = tt; } FOR(i, 0, 3) { if (p->child[i] == NULL) continue; DFS(p->child[i]); } p->tout = tt; } void DFS_Rev(TrieNode *p) { sort(p->all.begin(), p->all.end()); FOR(i, 0, 3) { if (p->child[i] == NULL) continue; DFS_Rev(p->child[i]); } } pii getRange(string s) { TrieNode *p = &T; FOR(i, 0, s.size()-1) { int c = convertChar(s[i]); if (p->child[c] == NULL) return mp(0, 0); p = p->child[c]; } return mp(p->tin, p->tout); } int getAns(string s, pii Range) { TrieNode *p = &T_rev; FOR(i, 0, s.size()-1) { int c = convertChar(s[i]); if (p->child[c] == NULL) return 0; p = p->child[c]; } int l = lower_bound(p->all.begin(), p->all.end(), Range.F) - p->all.begin(); int r = upper_bound(p->all.begin(), p->all.end(), Range.S) - p->all.begin()-1; return (r - l + 1); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); //freopen("INP.TXT", "r", stdin); cin >> n >> numQ; FOR(i, 1, n) cin >> S[i]; FOR(i, 1, n) add(S[i], i); DFS(&T); FOR(i, 1, n) reverse(S[i].begin(), S[i].end()); FOR(i, 1, n) add_rev(S[i], i); DFS_Rev(&T_rev); while (numQ--) { string P, Q; cin >> P >> Q; reverse(Q.begin(), Q.end()); pii R = getRange(P); cout <<getAns(Q, R)<<'\n'; } }
Compilation message
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 5 ms | 3448 KB | Output is correct |
2 | Correct | 5 ms | 3704 KB | Output is correct |
3 | Correct | 6 ms | 3704 KB | Output is correct |
4 | Correct | 5 ms | 3704 KB | Output is correct |
5 | Correct | 6 ms | 3704 KB | Output is correct |
6 | Correct | 5 ms | 3804 KB | Output is correct |
7 | Correct | 5 ms | 3804 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 427 ms | 225928 KB | Output is correct |
2 | Correct | 402 ms | 225928 KB | Output is correct |
3 | Correct | 421 ms | 225928 KB | Output is correct |
4 | Correct | 421 ms | 225928 KB | Output is correct |
5 | Correct | 498 ms | 252392 KB | Output is correct |
6 | Correct | 534 ms | 258192 KB | Output is correct |
7 | Correct | 135 ms | 258192 KB | Output is correct |
8 | Correct | 498 ms | 258192 KB | Output is correct |
9 | Correct | 413 ms | 258192 KB | Output is correct |
10 | Correct | 350 ms | 258192 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 37 ms | 258192 KB | Output is correct |
2 | Correct | 30 ms | 258192 KB | Output is correct |
3 | Correct | 98 ms | 258192 KB | Output is correct |
4 | Correct | 25 ms | 258192 KB | Output is correct |
5 | Correct | 33 ms | 258192 KB | Output is correct |
6 | Correct | 37 ms | 258192 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 5 ms | 3448 KB | Output is correct |
2 | Correct | 5 ms | 3704 KB | Output is correct |
3 | Correct | 6 ms | 3704 KB | Output is correct |
4 | Correct | 5 ms | 3704 KB | Output is correct |
5 | Correct | 6 ms | 3704 KB | Output is correct |
6 | Correct | 5 ms | 3804 KB | Output is correct |
7 | Correct | 5 ms | 3804 KB | Output is correct |
8 | Correct | 427 ms | 225928 KB | Output is correct |
9 | Correct | 402 ms | 225928 KB | Output is correct |
10 | Correct | 421 ms | 225928 KB | Output is correct |
11 | Correct | 421 ms | 225928 KB | Output is correct |
12 | Correct | 498 ms | 252392 KB | Output is correct |
13 | Correct | 534 ms | 258192 KB | Output is correct |
14 | Correct | 135 ms | 258192 KB | Output is correct |
15 | Correct | 498 ms | 258192 KB | Output is correct |
16 | Correct | 413 ms | 258192 KB | Output is correct |
17 | Correct | 350 ms | 258192 KB | Output is correct |
18 | Correct | 37 ms | 258192 KB | Output is correct |
19 | Correct | 30 ms | 258192 KB | Output is correct |
20 | Correct | 98 ms | 258192 KB | Output is correct |
21 | Correct | 25 ms | 258192 KB | Output is correct |
22 | Correct | 33 ms | 258192 KB | Output is correct |
23 | Correct | 37 ms | 258192 KB | Output is correct |
24 | Correct | 431 ms | 258192 KB | Output is correct |
25 | Correct | 409 ms | 258192 KB | Output is correct |
26 | Correct | 388 ms | 258192 KB | Output is correct |
27 | Correct | 423 ms | 258192 KB | Output is correct |
28 | Correct | 300 ms | 258192 KB | Output is correct |
29 | Correct | 118 ms | 258192 KB | Output is correct |