#include<bits/stdc++.h>
using namespace std;
#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<char>op;
unordered_map<int,bool>mp;
int d[mxn*ascii];
void add(string s){
int i=0,v=0;
while(i<sz(s)){
d[v]=max(d[v],sz(s)-i);
if(t[v][s[i]-'a']){
v=t[v][s[i++]-'a'];
}
else{
v=t[v][s[i++]-'a']=nxt++;
}
}
mp[v]=1;
}
int ans=0;
void dfs(int v){
priority_queue<pii,vector<pii>,greater<pii>>pq;
if(mp[v]){
op.pb('P');
ans++;
}
for(int i=0;i<26;i++){
if(t[v][i]){
pq.push({d[t[v][i]],i});
}
}
while(sz(pq)){
pii x=pq.top();
pq.pop();
op.pb(x.S+'a');
ans++;
dfs(t[v][x.S]);
op.pb('-');
ans++;
}
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int n;
cin>>n;
for(int i=0;i<n;i++){
string temp;
cin>>temp;
add(temp);
}
dfs(0);
while(op.back()=='-'){
op.pop_back();
ans--;
}
cout<<ans<<'\n';
for(auto i:op)cout<<i<<'\n';
}
/*
input:
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
300 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
2 ms |
852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1364 KB |
Output is correct |
2 |
Correct |
3 ms |
1620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
4228 KB |
Output is correct |
2 |
Correct |
18 ms |
8760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
10168 KB |
Output is correct |
2 |
Correct |
8 ms |
2540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
24688 KB |
Output is correct |
2 |
Correct |
150 ms |
59644 KB |
Output is correct |
3 |
Correct |
91 ms |
30384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
63 ms |
19816 KB |
Output is correct |
2 |
Correct |
178 ms |
69040 KB |
Output is correct |
3 |
Correct |
83 ms |
34104 KB |
Output is correct |
4 |
Correct |
197 ms |
65544 KB |
Output is correct |