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, 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 |
---|
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... |