제출 #546876

#제출 시각아이디문제언어결과실행 시간메모리
546876ussef_abdallahType Printer (IOI08_printer)C++14
10 / 100
47 ms37148 KiB
#include <bits/stdc++.h> using namespace std; const int ALPHABET_SIZE = 26; class TrieNode { public: TrieNode* children[ALPHABET_SIZE]; bool isLeaf; TrieNode() { for (int i = 0; i < ALPHABET_SIZE; i++) { children[i] = nullptr; } isLeaf = false; } void insert(string &word) { for (char &c : word) { if (children[c - 'a'] == nullptr) children[c - 'a'] = new TrieNode(); children[c - 'a']; } } }; class Trie { private: TrieNode* root; public: Trie() { root = new TrieNode(); } void insert(string &word) { TrieNode* cur = root; for (char &c : word) { if (cur->children[c - 'a'] == nullptr) cur->children[c - 'a'] = new TrieNode(); cur = cur->children[c - 'a']; } cur->isLeaf = true; } void traverse(string &longest, vector<char> &ans) { dfs(root, longest, ans, 0); } void dfs(TrieNode* node, string &longest, vector<char> &ans, int pos) { if (node == nullptr) return; if (node->isLeaf) ans.push_back('P'); for (int i = 0; i < ALPHABET_SIZE; i++) { if (node->children[i] != nullptr && i + 'a' != longest[pos]) { ans.push_back('a' + i); dfs(node->children[i], longest, ans, pos); ans.push_back('-'); } } if (node->children[longest[pos] - 'a']) { ans.push_back(longest[pos]); dfs(node->children[longest[pos] - 'a'], longest, ans, pos + 1); } } }; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; vector<string> a(n); string longest = ""; Trie* trie = new Trie(); for (int i = 0; i < n; ++i) { cin >> a[i]; if (a[i].length() > longest.length()) longest = a[i]; trie->insert(a[i]); } vector<char> ans; trie->traverse(longest, ans); cout << ans.size() << "\n"; for (auto &c : ans) { cout << c << "\n"; } return 0; }

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

printer.cpp: In member function 'void TrieNode::insert(std::string&)':
printer.cpp:22:29: warning: statement has no effect [-Wunused-value]
   22 |             children[c - 'a'];
      |             ~~~~~~~~~~~~~~~~^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...