#include <bits/stdc++.h>
using namespace std;
#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];
Trie trie[500005];
vector<char> r;
void recurse(ll idx){
FOR(i, 1, trie[idx].ct) r.push_back('P');
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('-');
}
}
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;
vector<char> v;
for(char c : r) {
cout << c << endl;
/*if(c == 'P'){
for(char c2 : v) cout << c2;
cout << endl;
}else if(c == '-'){
v.pop_back();
}else{
v.push_back(c);
}*/
}
}
/*
8
swan
swaddle
result
rewind
rewindle
switch
reswindle
reswindle
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
106076 KB |
Output is correct |
2 |
Correct |
31 ms |
105856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
106072 KB |
Output is correct |
2 |
Correct |
30 ms |
106072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
106068 KB |
Output is correct |
2 |
Correct |
30 ms |
106076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
106040 KB |
Output is correct |
2 |
Correct |
34 ms |
106076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
106064 KB |
Output is correct |
2 |
Correct |
39 ms |
106064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
47 ms |
106088 KB |
Output is correct |
2 |
Correct |
51 ms |
106248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
85 ms |
106324 KB |
Output is correct |
2 |
Correct |
186 ms |
106856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
167 ms |
107124 KB |
Output is correct |
2 |
Correct |
108 ms |
106496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
464 ms |
108640 KB |
Output is correct |
2 |
Correct |
833 ms |
111712 KB |
Output is correct |
3 |
Correct |
484 ms |
109260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
318 ms |
108152 KB |
Output is correct |
2 |
Execution timed out |
1008 ms |
112724 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |