#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define show(x,y) cout << y << " " << #x << endl;
#define show2(x,y,i,j) cout << y << " " << #x << " " << j << " " << #i << endl;
#define show3(x,y,i,j,p,q) cout << y << " " << #x << " " << j << " " << #i << " " << q << " " << #p << endl;
#define show4(x,y) for(auto it:x) cout << it << " "; cout << #y << endl;
typedef pair<int,int>pii;
typedef pair<pii,pii>pi2;
void solve(){
int n;
cin >> n;
string arr[n];
int sz=-1;
string maxi="";
for(int x=0;x<n;x++){
cin >> arr[x];
if((int)arr[x].length()>sz){
sz=(int)arr[x].length();
maxi=arr[x];
}
}
vector<string>v[30];
for(int x=0;x<n;x++){
int mini=min((int)arr[x].length(),sz);
int counter=0;
for(int y=0;y<mini;y++){
if(arr[x][y]==maxi[y]) counter++;
else break;
}
v[counter].push_back(arr[x]);
}
for(int x=0;x<30;x++) sort(v[x].begin(),v[x].end());
string last="";
vector<char>ans;
for(int x=0;x<30;x++){
for(auto it:v[x]){
int mini=min((int)it.length(),(int)last.length());
int target=-1;
for(int y=0;y<mini;y++){
if(it[y]==last[y]) target++;
else break;
}
while((int)last.size()>target+1){
ans.push_back('-');
last.pop_back();
}
for(int y=target+1;y<(int)it.length();y++){
ans.push_back(it[y]);
}
ans.push_back('P');
last=it;
}
}
cout << ans.size() << "\n";
for(auto it:ans){
cout << it << "\n";
}
}
int32_t main(){
ios::sync_with_stdio(0);
cin.tie(0);
//freopen("in.txt", "r", stdin);
int t=1;
//cin >> t;
while(t--){
solve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
848 KB |
Output is correct |
2 |
Correct |
7 ms |
1496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
1772 KB |
Output is correct |
2 |
Correct |
7 ms |
1752 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
3528 KB |
Output is correct |
2 |
Correct |
41 ms |
6252 KB |
Output is correct |
3 |
Correct |
24 ms |
5688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
3556 KB |
Output is correct |
2 |
Correct |
42 ms |
7228 KB |
Output is correct |
3 |
Correct |
27 ms |
6216 KB |
Output is correct |
4 |
Correct |
47 ms |
7116 KB |
Output is correct |