Submission #1103911

#TimeUsernameProblemLanguageResultExecution timeMemory
1103911dubabubaType Printer (IOI08_printer)C++14
80 / 100
1077 ms106168 KiB
#include <bits/stdc++.h> using namespace std; const int mxc = 26; struct trie { trie *chi[mxc]; int len; int cnt; int las; trie() { cnt = 0; for(int i = 0; i < mxc; i++) chi[i] = NULL; len = 0; } }; void insert(trie *root, string s) { for(char c : s) { if(root->chi[c - 'a'] == NULL) root->chi[c - 'a'] = new trie(); root = root->chi[c - 'a']; } root->len = s.size(); root->cnt++; } trie *root = new trie(); vector<char> ans; void maxer(int &MAX, int CMP) { if(MAX < CMP) MAX = CMP; } void dfs(trie *cur) { for(int i = 0; i < mxc; i++) { trie *nxt = cur->chi[i]; if(nxt == NULL) continue; dfs(nxt); maxer(cur->len, nxt->len); } } void solve(trie *cur) { while(cur->cnt) { ans.push_back('P'); cur->cnt--; } for(int i = 0; i < mxc; i++) { if(cur->chi[i] == NULL) continue; if(cur->chi[i]->len == cur->len) continue; ans.push_back('a' + i); solve(cur->chi[i]); ans.push_back('-'); } for(int i = 0; i < mxc; i++) { if(cur->chi[i] == NULL) continue; if(cur->chi[i]->len != cur->len) continue; ans.push_back('a' + i); solve(cur->chi[i]); ans.push_back('-'); } } int main() { int n; cin >> n; for(int i = 0; i < n; i++) { string s; cin >> s; insert(root, s); } dfs(root); solve(root); while(ans.back() != 'P') ans.pop_back(); cout << ans.size() << endl; for(char c : ans) cout << c << endl; 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...