#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define endl "\n"
#define TASK ""
#define II pair<int,int>
#define fi first
#define se second
#define MASK(i) (1 << (i))
#define BIT(i,x) (((x) >> (i)) & 1)
int MOD = 1e9 + 7;
int const N = 1e6 + 7;
struct node{
node * child[26];
int exits;
int d;
node(){
for(int i = 0;i < 26;i++) child[i] = NULL;
exits = 0;
d = 0;
}
};
node *root = new node();
struct Trie{
void insert(string s){
node *p = root;
for(auto f : s){
int c = f - 'a';
if(p -> child[c] == NULL) p -> child[c] = new node();
p = p -> child[c];
}
p -> exits = 1;
}
}T;
vector<char> res;
void dfs(node *p){
p -> d = 1;
for(int i = 0;i < 26;i++){
node *v = p -> child[i];
if(v != NULL){
dfs(p->child[i]);
p -> d = max(p -> d,v -> d + 1);
}
}
}
void dfs2(node *p){
if(p -> exits) res.push_back('P');
for(int i = 0;i < 26;i++){
node *v = p -> child[i];
if(v != NULL && p -> d > v -> d + 1){
res.push_back(char(i + 'a'));
dfs2(v);
}
}
for(int i = 0;i < 26;i++){
node *v = p -> child[i];
if(v != NULL && p -> d == v -> d + 1){
res.push_back(char(i + 'a'));
dfs2(v);
}
}
res.push_back('-');
}
void solve(){
int n;cin>>n;
for(int i = 1;i <= n;i++){
string s;cin>>s;
T.insert(s);
}
dfs(root);
dfs2(root);
while(!res.empty() && res.back() != 'P')
res.pop_back();
cout<<res.size()<<endl;
for(int i = 0;i < res.size();i++){
cout<<res[i]<<endl;
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);
// freopen(TASK".INP","r",stdin);
// freopen(TASK".OUT","w",stdout);
solve();
return 0;
}
//be
| # | 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... |