Submission #881636

#TimeUsernameProblemLanguageResultExecution timeMemory
881636shleepType Printer (IOI08_printer)C++17
30 / 100
71 ms72904 KiB
#include <bits/stdc++.h> using namespace std; // Trie vector<vector<int>> nex(500001, vector<int>(26)); vector<int> endpoint(500001); // marks if a word is done vector<int> depth(500001, -1); int num_nodes = 0; void trie_insert(string s) { int cur = 0; for (char c : s) { if (!nex[cur][c-'a']) nex[cur][c-'a'] = ++num_nodes; cur = nex[cur][c-'a']; } endpoint[cur]=1; } // Find max depth of all nodes in subtree int depth_dfs(int cur) { if (endpoint[cur]) { depth[cur]=1; return 1; } int deepest = 0; for (int i=0;i<26;i++) { if (!nex[cur][i]) continue; deepest = max(deepest, depth_dfs(nex[cur][i])); } depth[cur] = deepest+1; return deepest+1; } int main() { int N; cin>>N; vector<string> S(N); for (int i=0;i<N;i++) cin >> S[i]; for (string s : S) trie_insert(s); // Calculate depths of every node depth_dfs(0); // Perform backtracking dfs, while prioritizing shorter depths // (this ensures we end on the longest word, which minimizes moves) vector<char> ans; auto dfs = [&](auto &&self, int cur) -> void { // end of word (but dont terminate early, e.g. "cat,cats") if (endpoint[cur]) { ans.push_back('P'); } // Visit child nodes in order of their depths vector<array<int,3>> candidates; for (int i=0;i<26;i++) { if (!nex[cur][i]) continue; int nxt = nex[cur][i]; candidates.push_back({depth[nxt], nxt, i}); } sort(candidates.begin(),candidates.end()); for (auto [_,nxt,chr] : candidates) { ans.push_back((char)(chr+'a')); self(self, nxt); ans.push_back('-'); } }; dfs(dfs, 0); // Get rid of final '-'s (we don't need to clean up) while (ans.back() == '-') ans.pop_back(); // Print results cout << ans.size() << '\n'; for (char c : ans) cout << c << '\n'; }
#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...