제출 #784878

#제출 시각아이디문제언어결과실행 시간메모리
784878vjudge1Selling RNA Strands (JOI16_selling_rna)C++17
100 / 100
222 ms272532 KiB
#include <bits/stdc++.h> using namespace std; int chrToHash[256]{}; int n, m; vector<string> str; vector<int> results; struct TrieNode { int cnt = 0; int l = INT_MAX, r = INT_MIN; vector<int> v; string* ogString{}; TrieNode* children[4]{}; TrieNode* parent{}; void depthFirstSearch(int& timer); }; vector<TrieNode*> trieNodes; void TrieNode::depthFirstSearch(int& timer) { l = timer, timer += cnt; for (int i = 0; i < cnt; i++) trieNodes.push_back(this); for (auto &child : children) { if (child == nullptr) continue; child->depthFirstSearch(timer); l = min(child->l, l); r = max(child->r, r); } r = timer; } struct Trie { TrieNode *root = new TrieNode; void insert(string& s) { auto curr = root; int size = (int)s.size(); for (int i = 0; i < size; i++) { int d = chrToHash[s[i]]; if (curr->children[d] == nullptr) curr->children[d] = new TrieNode; curr = curr->children[d]; } curr->ogString = &s; curr->cnt++; } TrieNode* getNode(const string& s) { auto curr = root; int size = (int)s.size(); for (int i = 0; i < size; i++) { int d = chrToHash[s[i]]; if (curr->children[d] == nullptr) return nullptr; curr = curr->children[d]; } return curr; } void process() { int timer = 0; root->depthFirstSearch(timer); } } trie; struct Eirt { TrieNode *root = new TrieNode; void insert(string& s, const int& index) { auto curr = root; int size = (int)s.size(); for (int i = size - 1; i >= 0; i--) { int d = chrToHash[s[i]]; if (curr->children[d] == nullptr) { curr->children[d] = new TrieNode; curr->children[d]->parent = curr; } curr = curr->children[d]; } curr->ogString = &s; curr->v.push_back(index); while (curr != root) { curr = curr->parent; curr->v.push_back(index); } } TrieNode* getNode(const string& s) { auto curr = root; int size = (int)s.size(); for (int i = size - 1; i >= 0; i--) { int d = chrToHash[s[i]]; if (curr->children[d] == nullptr) return nullptr; curr = curr->children[d]; } return curr; } } eirt; int main() { // freopen("inp.txt", "r", stdin); // freopen("out.txt", "w", stdout); ios::sync_with_stdio(false); chrToHash['A'] = 0; chrToHash['C'] = 1; chrToHash['G'] = 2; chrToHash['U'] = 3; cin >> n >> m; str.resize(n); for (int i = 0; i < n; i++) { cin >> str[i]; trie.insert(str[i]); } trie.process(); for (int i = 0; i < n; i++) eirt.insert(*trieNodes[i]->ogString, i); results.resize(m); string pref, suff; for (int q = 0; q < m; q++) { cin >> pref >> suff; auto itr = trie.getNode(pref); auto jtr = eirt.getNode(suff); if (itr == nullptr || jtr == nullptr) { results[q] = 0; continue; } auto l = lower_bound(jtr->v.begin(), jtr->v.end(), itr->l); auto r = lower_bound(jtr->v.begin(), jtr->v.end(), itr->r); results[q] = r - l; } for (auto &result : results) cout << result << '\n'; }

컴파일 시 표준 에러 (stderr) 메시지

selling_rna.cpp: In member function 'void Trie::insert(std::string&)':
selling_rna.cpp:50:35: warning: array subscript has type 'char' [-Wchar-subscripts]
   50 |             int d = chrToHash[s[i]];
      |                                   ^
selling_rna.cpp: In member function 'TrieNode* Trie::getNode(const string&)':
selling_rna.cpp:65:35: warning: array subscript has type 'char' [-Wchar-subscripts]
   65 |             int d = chrToHash[s[i]];
      |                                   ^
selling_rna.cpp: In member function 'void Eirt::insert(std::string&, const int&)':
selling_rna.cpp:90:35: warning: array subscript has type 'char' [-Wchar-subscripts]
   90 |             int d = chrToHash[s[i]];
      |                                   ^
selling_rna.cpp: In member function 'TrieNode* Eirt::getNode(const string&)':
selling_rna.cpp:113:35: warning: array subscript has type 'char' [-Wchar-subscripts]
  113 |             int d = chrToHash[s[i]];
      |                                   ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...