Submission #880625

#TimeUsernameProblemLanguageResultExecution timeMemory
880625OAleksaType Printer (IOI08_printer)C++14
30 / 100
107 ms84728 KiB
#include<bits/stdc++.h> using namespace std; #define int long long #define f first #define s second vector<char> ans; const int maxn = 5e5 + 69; int trie[maxn][26], kraj[maxn], sz[maxn]; int n, node; void add(string s) { int m = s.size(); int t = 0; for (int i = 0;i < m;i++) { int x = s[i] - 'a'; if (trie[t][x] == 0) trie[t][x] = ++node; t = trie[t][x]; } kraj[t]++; } void dfs(int v) { sz[v] = 1; for (int i = 0;i < 26;i++) { if (trie[v][i]) { dfs(trie[v][i]); sz[v] += sz[trie[v][i]]; } } } void dfs1(int v) { for (int i = 0;i < kraj[v];i++) ans.push_back('P'); vector<pair<int, int>> x; for (int i = 0;i < 26;i++) { if (trie[v][i]) x.push_back({sz[trie[v][i]], i}); } sort(x.begin(), x.end()); for (int i = 0;i < (int)x.size();i++) { ans.push_back((char)(x[i].s + 'a')); dfs1(trie[v][x[i].s]); ans.push_back('-'); } } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int tt = 1; //cin >> tt; while (tt--) { cin >> n; for (int i = 0;i < n;i++) { string s; cin >> s; add(s); } dfs(0); dfs1(0); while (ans.back() == '-') ans.pop_back(); 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...