// Problem: B. 6-7 Triangles
// Contest: Codeforces - MACSO Fall 2025. Qualification Stage
// URL: https://codeforces.com/group/mlc657bZIV/contest/645262/problem/B
// Memory Limit: 256 MB
// Time Limit: 1000 ms
//
//
// Powered by CP Editor (https://cpeditor.org)
#include <bits/stdc++.h>
using namespace std;
#define print(l) for(auto i:l) cout<<i<<" ";cout<<endl;
#define input(t,l,n) vector<t>l(n);for(int i = 0;i<n;i++)cin>>l[i];
#define int long long
#define pb push_back
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define all(l) l.begin(),l.end()
#define pii pair<int,int>
#define fi first
#define se second
const int M = 1e9+7;
const int inf = 1e18;
int bp(int x, int y, int p){
int res = 1;
x = x % p;
while (y > 0) {
if (y & 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
int MI(int n, int p){
return bp(n, p - 2, p);
}
int mul(int x,int y, int p){
return x * 1ull * y % p;
}
int di(int x,int y, int p){
return mul(x, MI(y, p), p);
}
int n , m , k , q;
//3
//print
//the
//poem
//vector<vector<int>>
void solve(int testcase_number){
cin>>n;
input(string,l,n);
sort(all(l));
reverse(all(l));
// print(l);
vector<char>ans;
vector<char>curr;
char kk;
int ms = 0;
for(auto i:l){
if((int)i.size() >= ms){
ms = i.size();
kk = i[0];
}
}
vector<string>ls,pq;
for(auto i:l){
if(i[0] != kk){
ls.pb(i);
}
else{
pq.pb(i);
}
}
// cout<<'P'<<endl;
//int sz = curr.size();
n = ls.size();
sort(all(ls));
for(auto i:ls[0]){
curr.pb(i);
ans.pb(i);
// cout<<i<<endl;
}
ans.pb('P');
for(int i = 1;i<n;i++){
int p1 = 0;
while(p1< (int)ls[i].size() and ls[i][p1] == curr[p1]){
p1++;
}
while((int)curr.size() > p1 and curr.size()){
curr.pop_back();
ans.pb('-');
// cout<<'-'<<endl;
}
while(p1<(int)ls[i].size()){
curr.pb(ls[i][p1]);
ans.pb(curr.back());
// cout<<curr.back()<<endl;
p1++;
}
ans.pb('P');
// cout<<'P'<<endl;
}
n = pq.size();
sort(all(pq));
// print(pq);
// print(curr);
for(int i = 0;i<n;i++){
int p1 = 0;
while(p1<pq[i].size() and p1< curr.size() and pq[i][p1] == curr[p1]){
p1++;
}
// cout<<p1<<endl;
while((int)curr.size() > p1){
curr.pop_back();
ans.pb('-');
// cout<<'-'<<endl;
}
while(p1<(int)pq[i].size()){
curr.pb(pq[i][p1]);
ans.pb(curr.back());
// cout<<curr.back()<<endl;
p1++;
}
ans.pb('P');
// cout<<'P'<<endl;
}
cout<<ans.size()<<endl;
for(auto i:ans){
cout<<i<<endl;
}
}
signed main(){
ios::sync_with_stdio(0);//DO NOT USE IN INTERACTIVE
cin.tie(0), cout.tie(0);
cout << fixed<<setprecision(9);
int t = 1;
//cin>>t;
for(int i = 1;i<=t;i++){
solve(i);
}
}
| # | 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... |