Submission #855075

#TimeUsernameProblemLanguageResultExecution timeMemory
855075vjudge1Type Printer (IOI08_printer)C++17
20 / 100
66 ms50128 KiB
#include <bits/stdc++.h> using namespace std; bool vis1[500005]; int t[500005][26];//o --> ch int t2[500005];//i的深度 set<pair<int,int>> t1[500005];//i的子树的深度排序后 int k,ans1=0; struct Trie { int n = 1; void update(string s) { int o = 1; for (int i = 0; i < (int)s.size(); i++) { if (!t[o][s[i] - 'a']) t[o][s[i] - 'a'] = ++n, o = n; else o = t[o][s[i] - 'a']; } vis1[o] = 1; // 结尾 return; } int dfs1(int o) { int flag=0; for(int i=0;i<26;i++){ if(t[o][i]!=0){ flag=1; int d=dfs1(t[o][i]);//o子树i的深度 t1[o].insert({d,i}); t2[o]+=d; } } if(!flag)return 1; return t2[o]; } void dfs(int o, vector<char>& ans) { if(vis1[o]==1)ans1++,ans.push_back('P'); for(auto i:t1[o]){ ans.push_back(i.second+'a'); dfs(t[o][i.second],ans); } if(ans1!=k)ans.push_back('-'); return; } }; int main() { cin >> k; string s; Trie tr; for (int i = 0; i < k; i++) { cin >> s; tr.update(s); } tr.dfs1(1); vector<char> ans; tr.dfs(1, ans); cout << ans.size(); cout << "\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...