Submission #328674

#TimeUsernameProblemLanguageResultExecution timeMemory
328674aris12345678Type Printer (IOI08_printer)C++14
100 / 100
198 ms104160 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long #define pi pair<int, int> #define fi first #define se second const int mxN = 1e5+5; const int mod = 1e9+7; const ll inf = 1e18; struct node { bool word; int depth; node *children[26]; node() { word = 0, depth = 0; for(int i = 0; i < 26; i++) children[i] = nullptr; } }; node *root = new node(); int n; string s[mxN]; vector<char> ans; // insert t void add(string t) { int len = t.length(); node *cur = root; for(int i = 0; i < len; i++) { cur->depth = max(cur->depth, len); if(cur->children[t[i]-'a'] == nullptr) cur->children[t[i]-'a'] = new node(); cur = cur->children[t[i]-'a']; } cur->depth = max(cur->depth, len); cur->word = 1; } // dfs in a trie starting from the root void dfs(node *u, int back) { if(u->word) ans.push_back('P'); // red vertice - end of the word int p = 0; node *x = nullptr; for(int i = 0; i < 26; i++) { node *v = u->children[i]; if(v == nullptr) continue; if(x == nullptr || v->depth > x->depth) { x = v; p = i; } } for(int i = 0; i < 26; i++) { node *v = u->children[i]; if(v == x || v == nullptr) continue; ans.push_back(i+'a'); dfs(v, 0); } if(x != nullptr) { ans.push_back(p+'a'); dfs(x, back); } if(!back) ans.push_back('-'); } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n; for(int i = 0; i < n; i++) { cin >> s[i]; add(s[i]); } dfs(root, 1); cout << ans.size() << "\n"; for(char i : ans) cout << i << "\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...