Submission #940117

#TimeUsernameProblemLanguageResultExecution timeMemory
940117thinknoexitType Printer (IOI08_printer)C++17
100 / 100
97 ms50700 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; const int N = 500500; int trie[N][26]; bool ch[N]; int mxd[N]; string ans = ""; void dfs_sz(int v) { for (int i = 0;i < 26;i++) { if (trie[v][i]) { dfs_sz(trie[v][i]); mxd[v] = max(mxd[v], mxd[trie[v][i]] + 1); } } } void dfs(int v, int keep) { if (ch[v]) ans += "P"; if (keep) { int big = -1; for (int i = 0;i < 26;i++) { if (trie[v][i]) { if (mxd[trie[v][i]] == mxd[v] - 1) big = i; } } for (int i = 0;i < 26;i++) { if (trie[v][i] && trie[v][i] != trie[v][big]) { ans += i + 'a'; dfs(trie[v][i], 0); } } if (big != -1) { ans += big + 'a'; dfs(trie[v][big], 1); } return; } for (int i = 0;i < 26;i++) { if (trie[v][i]) { ans += i + 'a'; dfs(trie[v][i], 0); } } ans += "-"; } int main() { cin.tie(nullptr)->sync_with_stdio(false); int n; cin >> n; int idx = 0; for (int i = 1;i <= n;i++) { string s; cin >> s; int now = 0; for (auto& x : s) { if (!trie[now][x - 'a']) trie[now][x - 'a'] = ++idx; now = trie[now][x - 'a']; } ch[now] = 1; } dfs_sz(0); dfs(0, 1); cout << ans.size() << '\n'; for (auto& x : ans) cout << x << '\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...