제출 #990756

#제출 시각아이디문제언어결과실행 시간메모리
990756serkanrashidType Printer (IOI08_printer)C++14
10 / 100
264 ms89536 KiB
#include<bits/stdc++.h> #define endl "\n" using namespace std; int inf = 1e9; struct Trie { Trie *p[26]; Trie *par; int is_end; int sub; Trie() { is_end = 0; sub = inf; par = nullptr; for(int i = 0; i < 26; i++) p[i] = nullptr; } }; int n; Trie *root = new Trie(); int br; vector<char>ans; void insert1(string s) { Trie *beg = root; for(char c : s) { if(beg->p[c-'a']==nullptr) beg->p[c-'a'] = new Trie(); (beg->p[c-'a']) -> par = beg; beg = beg->p[c-'a']; } (beg->is_end) ++; beg->sub = 0; while(beg->par!=nullptr) { int ch = beg->sub; beg = beg->par; beg->sub = min(beg->sub,ch+1); } } void read() { cin >> n; string s; for(int i = 1; i <= n; i++) { cin >> s; insert1(s); } } bool cmp(pair<Trie*,char> t1, pair<Trie*,char> t2) { if(t1.first==nullptr||t2.first==nullptr) return t1.first != nullptr; return ((t1.first)->sub) < ((t2.first)->sub); } void rec(Trie *beg) { for(int i = 1; i <= (beg->is_end); i++) { ans.push_back('P'); br++; } if(br==n) { cout << ans.size() << endl; for(char c : ans) cout << c << endl; exit(0);///VAZHNO } vector<pair<Trie*,char> >v; for(int i = 0; i < 26; i++) v.push_back({beg->p[i],i+'a'}); sort(v.begin(),v.end(),cmp); for(pair<Trie*,char> pom : v) { if(pom.first==nullptr) return; ans.push_back(pom.second); rec(pom.first); ans.push_back('-'); } } void solve() { rec(root); } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); read(); solve(); 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...