#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;
Trie trie[500005];
vector<char> r[26];
void recurse(ll idx, ll t){
FOR(i, 0, 25){
if(trie[idx].nx[i] != -1) {
r[t].push_back('a' + i);
recurse(trie[idx].nx[i], t);
r[t].push_back('-');
}
}
FOR(i, 1, trie[idx].ct) r[t].push_back('P');
}
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++;
}
ll s = 0, mx = 0, mxid = -1;
FOR(i, 0, 25){
if(trie[0].nx[i] != -1) {
r[i].push_back('a' + i);
recurse(trie[0].nx[i], i);
r[i].push_back('-');
}
s += r[i].size();
ll mxi = 0, j = (int)r[i].size() - 1;
while(j >= 0 && r[i][j] == '-'){
j--;
mxi++;
}
if(mxi > mx){
mx = mxi;
mxid = i;
}
}
cout << (s - mx) << endl;
FOR(i, 0, 25){
if(i != mxid){
for(char c : r[i]){
cout << c << endl;
}
}
}
if(mxid != -1){
FOR(j, 0, (int)r[mxid].size() - 1 - mx){
cout << r[mxid][j] << endl;
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
109836 KB |
Output is correct |
2 |
Correct |
30 ms |
109808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
109912 KB |
Output is correct |
2 |
Correct |
38 ms |
109780 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
28 ms |
109780 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
109812 KB |
Output is correct |
2 |
Incorrect |
32 ms |
109916 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
29 ms |
109908 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
44 ms |
109916 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
90 ms |
110164 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
166 ms |
110444 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
381 ms |
111224 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
301 ms |
110932 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |