Submission #839494

#TimeUsernameProblemLanguageResultExecution timeMemory
839494LilintaType Printer (IOI08_printer)C++14
100 / 100
122 ms106424 KiB
// IOI 2008 Day 1 Problem 1 - Type Printer // Problem link: https://oj.uz/problem/view/IOI08_printer #include <bits/stdc++.h> using namespace std; struct Trie { struct Node { Node* child[26]; bool exist = false; int maxDepth = 0; int deepest = -1; Node() { for (int c = 0; c < 26; ++c) { child[c] = nullptr; } } }; Node* root; Trie() { root = new Node(); } void addString(string &s) { Node* p = root; int n = s.size(); for (int i = 0; i < n; ++i) { int c = s[i] - 'a'; if (!(p -> child[c])) { p -> child[c] = new Node(); } if (n - i > p -> maxDepth) { p -> maxDepth = n - i; p -> deepest = c; } p = p -> child[c]; } p -> exist = true; } /* void selfDelete(Node* p) { for (int c = 0; c < 26; ++c) { if (p -> child[c]) { selfDelete(p -> child[c]); } } delete p; } */ }; int n; Trie T; vector<char> output; void dfs(Trie::Node* p) { if (p -> exist) { output.push_back('P'); } for (int c = 0; c < 26; ++c) { if (c != p -> deepest && p -> child[c]) { output.push_back(c + 'a'); dfs(p -> child[c]); output.push_back('-'); } } if (p -> deepest != -1) { output.push_back(p -> deepest + 'a'); dfs(p -> child[p -> deepest]); output.push_back('-'); } delete p; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for (int i = 0; i < n; ++i) { string s; cin >> s; T.addString(s); } dfs(T.root); while (output.back() == '-') { output.pop_back(); } cout << output.size() << '\n'; for (char c : output) { cout << c << '\n'; } return 0; }
#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...