Submission #855148

#TimeUsernameProblemLanguageResultExecution timeMemory
855148vjudge1Type Printer (IOI08_printer)C++11
100 / 100
78 ms51212 KiB
#include <bits/stdc++.h> #define _for(i, a, b) for (int i = (a); i < (int)(b); ++i) using namespace std; const int SIG = 26, NN = 25004; struct Trie { struct Node { int ch[SIG], mark, end; } T[NN * 20]; int sz = 0; void insert(const string& s) { int u = 0; for (char c : s) { int d = c - 'a', &cu = T[u].ch[d]; if (!cu) cu = ++sz; u = cu; } T[u].end = true; } void dfs(int u, string& ans) { const Node& x = T[u]; if (x.end) ans += 'P'; int mi = -1; for (int i = 0; i < SIG; ++i) { int v = x.ch[i]; if (!v) continue; if (T[v].mark) mi = i; else ans += i + 'a', dfs(v, ans); } if (mi != -1) ans += mi + 'a', dfs(x.ch[mi], ans); ans += '-'; } } Tr; int main(void) { ios::sync_with_stdio(false), cin.tie(0); int n; cin >> n; string s, l; _for(i, 0, n) { cin >> s, Tr.insert(s); if (s.length() > l.length()) l = s; } int u = 0; _for(i, 0, l.size()) u = Tr.T[u].ch[l[i] - 'a'], Tr.T[u].mark = true; s.clear(), Tr.dfs(0, s); while (s.back() == '-') s.pop_back(); cout << s.size() << "\n"; for (char c : s) 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...