#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 |
1 |
Correct |
38 ms |
106064 KB |
Output is correct |
2 |
Correct |
33 ms |
106076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
106072 KB |
Output is correct |
2 |
Correct |
31 ms |
106068 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
34 ms |
105964 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
106076 KB |
Output is correct |
2 |
Incorrect |
38 ms |
106068 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
40 ms |
106096 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
55 ms |
106068 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
87 ms |
106508 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
181 ms |
107212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
405 ms |
108484 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
315 ms |
108052 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |