#include <bits/stdc++.h>
#define F first
#define S second
typedef long long ll;
using namespace std;
const int N=1e6+1,mod=1e9+7;
int trie[N][26],mxd[N];
char c[N];
bool leaf[N];
int n;
int sz=0;
bool cmp(int a,int b){
return mxd[a]<mxd[b];
}
void insert(string t){
int node=0,siz=t.size();
for(int i=0;i<siz;i++){
mxd[node]=max(mxd[node],siz-i);
if(trie[node][t[i]-'a']==0)trie[node][t[i]-'a']=++sz;
node=trie[node][t[i]-'a'];
c[node]=t[i];
}
leaf[node]=1;
}
int cnt=0;
bool stop=0;
vector<char>ans;
void dfs(int node=0){
if(leaf[node]){
ans.push_back('P');
cnt++;
if(cnt==n)
stop=1;
}
sort(trie[node],trie[node]+26,cmp);
for(int i=0;i<26;i++){
if(!trie[node][i])continue;
ans.push_back(c[trie[node][i]]);
dfs(trie[node][i]);
}
if(!stop)
ans.push_back('-');
}
int main()
{
cin>>n;
for(int i=0;i<n;i++){
string t;
cin>>t;
insert(t);
}
dfs();
cout<<ans.size()<<'\n';
for(auto i:ans)
cout<<i<<'\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... |