Submission #531591

#TimeUsernameProblemLanguageResultExecution timeMemory
531591erkeType Printer (IOI08_printer)C++11
100 / 100
136 ms120144 KiB
#include <bits/stdc++.h> using namespace std; struct Node { bool isLeaf; vector<Node*> adj; int dep, far; Node(): isLeaf(false), adj(26, nullptr), dep(0), far(-1) {} }; struct Trie { Node* root; Trie() { root = new Node; } void insert(Node* &node, int pos, string &s) { if (node == nullptr) node = new Node; if (pos == (int) s.size()) { node->isLeaf = true; return; } insert(node->adj[s[pos] - 'a'], pos + 1, s); node->dep = max(node->dep, node->adj[s[pos] - 'a']->dep + 1); if (node->far == -1 || node->adj[s[pos] - 'a']->dep > node->adj[node->far]->dep) { node->far = s[pos] - 'a'; } } void insert(string &s) { insert(root, 0, s); } }; void dfs(Node* node, vector<char> &res) { if (node->isLeaf) { res.push_back('P'); } for (int i = 0; i < 26; i++) { if (node->adj[i] != nullptr && i != node->far) { res.push_back((char)('a' + i)); dfs(node->adj[i], res); res.push_back('-'); } } if (node->far != -1) { res.push_back((char)('a' + node->far)); dfs(node->adj[node->far], res); res.push_back('-'); } } int main() { cin.tie(0)->sync_with_stdio(0); int n; cin >> n; Trie trie; for (int i = 0; i < n; i++) { string s; cin >> s; trie.insert(s); } vector<char> res; dfs(trie.root, res); while (res.back() == '-') { res.pop_back(); } cout << res.size() << '\n'; for (auto &i : res) { cout << i << '\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...