#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, prev_idx = -1;
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;
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[trie_idx].prev_idx = ptr;
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 |
1 |
Correct |
33 ms |
109904 KB |
Output is correct |
2 |
Correct |
33 ms |
109940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
109916 KB |
Output is correct |
2 |
Correct |
31 ms |
109916 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
31 ms |
109944 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
109916 KB |
Output is correct |
2 |
Incorrect |
29 ms |
110000 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
30 ms |
109868 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
42 ms |
109928 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
95 ms |
110252 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
163 ms |
111052 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
369 ms |
112252 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
310 ms |
112072 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |