This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define MAXN 25005
#define ll long long
#define FOR(i, a, b) for(ll i = a; i <= b; i++)
struct Trie{
ll nx[26], ct = 0;
Trie () {
FOR(i, 0, 25) nx[i] = -1;
}
};
ll n, trie_idx = 1, rmv[500005], rmve = 0;
Trie trie[500005];
vector<char> r;
void recurse(ll idx){
FOR(i, 0, 25){
if(i != rmv[idx] && trie[idx].nx[i] != -1) {
r.push_back('a' + i);
recurse(trie[idx].nx[i]);
r.push_back('-');
}
}
if(rmv[idx] != -1){
r.push_back('a' + rmv[idx]);
recurse(trie[idx].nx[rmv[idx]]);
r.push_back('-');
}
FOR(i, 1, trie[idx].ct) r.push_back('P');
}
ll ct_rmv(ll idx){
rmv[idx] = -1;
ll mx_rmv = -1;
FOR(i, 0, 25){
if(trie[idx].nx[i] != -1) {
ll cti = ct_rmv(trie[idx].nx[i]);
if(cti >= mx_rmv){
rmv[idx] = i;
mx_rmv = cti;
}
}
}
// cout << idx << " => " << rmv[idx] << endl;
if(mx_rmv == -1) return 1;
return mx_rmv + 1;
}
int main(){
cin >> n;
FOR(i, 1, n){
string s;
cin >> s;
ll ptr = 0;
for(char c : s){
if(trie[ptr].nx[c - 'a'] == -1){
trie[ptr].nx[c - 'a'] = trie_idx;
trie_idx++;
}
ptr = trie[ptr].nx[c - 'a'];
}
trie[ptr].ct++;
}
ct_rmv(0);
recurse(0);
while(r.size() && r.back() == '-') r.pop_back();
cout << r.size() << endl;
for(char c : r) cout << c << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |