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;
vector<char> res;
int n;
string s;
struct node{
int cnt, dep;
vector<int> nx;
node(){
cnt = dep = 0;
nx.resize(26, -1);
}
};
vector<node> vec;
void add(string s){
int root = 0;
for (auto x : s){
int val = x - 'a';
if (vec[root].nx[val] == -1){
vec[root].nx[val] = (int) vec.size();
vec.push_back(node());
}
root = vec[root].nx[val];
}
vec[root].cnt++;
}
void dfs(int root){
for (int i = 1; i <= vec[root].cnt; i++){
res.push_back('P');
}
int ma = 0, pos = -1;
for (int i = 0; i < 26; i++){
if (vec[root].nx[i] != -1){
int nw = vec[root].nx[i];
if (vec[nw].dep > ma){
ma = vec[nw].dep;
pos = i;
}
}
}
for (int i = 0; i < 26; i++){
if (vec[root].nx[i] != -1 && i != pos){
res.push_back(char(i + 'a'));
dfs(vec[root].nx[i]);
}
}
if (pos != -1) {res.push_back(char(pos + 'a'));
dfs(vec[root].nx[pos]);}
res.push_back('-');
}
void dfs2(int root){
vec[root].dep = 1;
for (int i = 0; i < 26; i++){
if (vec[root].nx[i] != -1){
dfs2(vec[root].nx[i]);
vec[root].dep = max(vec[root].dep, vec[vec[root].nx[i]].dep + 1);
}
}
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(NULL);
vec.push_back(node());
cin >> n;
for (int i = 1; i <= n; i++){
cin >> s;
add(s);
}
dfs2(0);
dfs(0);
while (res.back() == '-') res.pop_back();
cout << (int) res.size() << "\n";
for (auto x : res){
cout << x << "\n";
}
return 0;
}
# | 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... |