Submission #686710

#TimeUsernameProblemLanguageResultExecution timeMemory
686710finn__Type Printer (IOI08_printer)C++17
100 / 100
194 ms99568 KiB
#include <bits/stdc++.h> using namespace std; struct Node { Node *ch[26]; unsigned height = 0; bool z = 0; }; void trie_insert(Node *x, string const &s, size_t i = 0) { if (i == s.size()) x->z = 1; else { if (!x->ch[s[i] - 'a']) { x->ch[s[i] - 'a'] = (Node *)calloc(1, sizeof(Node)); } trie_insert(x->ch[s[i] - 'a'], s, i + 1); x->height = max(x->height, x->ch[s[i] - 'a']->height + 1); } } void trie_destroy(Node *x) { for (unsigned i = 0; i < 26; i++) if (x->ch[i]) { trie_destroy(x->ch[i]); free(x->ch[i]); } } void trie_dfs(Node *x, string &traversal) { if (x->z) traversal.push_back('P'); vector<pair<unsigned, unsigned>> children; for (unsigned i = 0; i < 26; i++) { if (x->ch[i]) { children.emplace_back(x->ch[i]->height, i); } } sort(children.begin(), children.end()); for (auto const &[ignore, i] : children) { if (x->ch[i]) { traversal.push_back(i + 'a'); trie_dfs(x->ch[i], traversal); traversal.push_back('-'); } } } int main() { size_t n; cin >> n; Node root; memset(root.ch, 0, sizeof root.ch); for (size_t i = 0; i < n; i++) { string s; cin >> s; trie_insert(&root, s); } string traversal; trie_dfs(&root, traversal); trie_destroy(&root); while (traversal.back() == '-') traversal.pop_back(); cout << traversal.size() << '\n'; for (char const &c : traversal) cout << c << '\n'; }
#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...