#include<bits/stdc++.h>
//#include<bits/extc++.h>
#define fi first
#define se second
#define int long long
using namespace std;
//using namespace __gnu_pbds;
using db=double;
using ll=int64_t;
using sll=__int128;
using lb=long double;
mt19937 rng(random_device{}());
const int maxn=1e6+10; int trie[maxn][26]; int stp[maxn];
int depth[maxn]; int mx[maxn]; int idx=0; vector<char>ans;
void update(string s){
int p=0;
for(int i=0; i<s.size(); i++){
int c=s[i]-'a';
if(!trie[p][c]){
idx++; trie[p][c]=idx;
}
p=trie[p][c];
}
stp[p]++;
}
void dfsdeep(int node){
for(int i=0; i<26; i++){
if(trie[node][i]){
int x=trie[node][i]; dfsdeep(x);
if(depth[x]+1>depth[node]){
depth[node]=depth[x]+1; mx[node]=i;
}
}
}
}
void dfs(int node){
for(int i=0; i<stp[node]; i++){
ans.push_back('P');
}
for(int i=0; i<26; i++){
if(i!=mx[node] && trie[node][i]!=0){
ans.push_back(char(i+'a')); dfs(trie[node][i]);
}
}
if(mx[node]!=-1){
ans.push_back(char(mx[node]+'a')); dfs(trie[node][mx[node]]);
}
ans.push_back('-');
}
int32_t main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n; cin>>n; memset(mx,-1,sizeof(mx));
for(int i=0; i<n; i++){
string s; cin>>s; update(s);
}
dfsdeep(0); dfs(0);
while(ans.back()=='-')ans.pop_back();
cout<<ans.size()<<'\n';
for(auto x:ans)cout<<x<<'\n';
}
/*
Overhead the albatross hangs motionless upon the air
And deep beneath the rolling waves in labyrinths of coral caves
The echo of a distant time comes willowing across the sand
And everything is green and submarine
And no one showed us to the land
And no one knows the wheres or whys
But something stirs and something tries
And starts to climb towards the light
*/
| # | 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... |