#include<bits/stdc++.h>
using namespace std;
#define int long long
#define F first
#define S second
#define all(x) x.begin(),x.end()
#define pii pair<int,int>
#define pb push_back
#define sz(x) (int)(x.size())
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
#define vi vector<int>
#define vp vector<pii>
#define vvi vector<vi>
const int mxn=25005;
const int ascii=26;
int t[mxn*ascii][ascii];
int nxt=1;
vector<string>s;
vector<vector<char>>op(26);
int mxlen[26];
void add(string s){
int i=0,v=0;
while(i<sz(s)){
if(t[v][s[i]-'a']){
v=t[v][s[i++]-'a'];
}
else{
v=t[v][s[i++]-'a']=nxt++;
}
}
}
int ans=0;
void dfs(int v,int cnt){
bool used=0;
for(int i=0;i<26;i++){
if(t[v][i]){
used=1;
if(v==0){
op[i].pb(i+'a');
ans++;
dfs(t[v][i],i);
op[i].pb('-');
ans++;
}
else{
op[cnt].pb(i+'a');
ans++;
dfs(t[v][i],cnt);
op[cnt].pb('-');
ans++;
}
}
}
if(!used){
op[cnt].pb('P');
ans++;
}
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int n;
cin>>n;
pii mx={0,0};
for(int i=0;i<n;i++){
string temp;
cin>>temp;
add(temp);
chmax(mxlen[temp[0]-'a'],sz(temp));
mx=max(mx,{mxlen[temp[0]-'a'],temp[0]-'a'});
}
dfs(0,-1);
cout<<ans-mx.F<<'\n';
for(int i=0;i<26;i++){
if(i==mx.S)continue;
for(auto j:op[i]){
cout<<j<<'\n';
}
}
for(int i=0;i<sz(op[mx.S])-mx.F;i++){
cout<<op[mx.S][i]<<'\n';
}
}
/*
input:
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
340 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
didn't print every word |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
1620 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
5460 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
18 ms |
13652 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
41 ms |
33928 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
33 ms |
26580 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |