This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#define db double
#define fo(i,d,c) for(int i=d;i<=c;i++)
#define fod(i,c,d) for(int i=c;i>=d;i--)
#define maxn 1000010
#define N 1010
#define fi first
#define se second
#define pb push_back
#define en cout<<"\n";
#define int long long
#define inf 1e9
#define pii pair<int,int>
#define vii vector<pii>
#define bit(i,j) ((i>>j)&1)
#define offbit(i,j) (i^(1<<j))
#define onbit(i,j) (i|(j<<1))
#define vi vector<int>
#define vvi vector<vector<int>>
#define checkfile(FiLeNaMe) { if(fopen(FiLeNaMe".inp","r")) freopen(FiLeNaMe".inp","r",stdin),freopen(FiLeNaMe".out","w",stdout); }
template <typename T1, typename T2> bool minimize(T1 &a, T2 b){if (a > b) {a = b; return true;} return false;}
template <typename T1, typename T2> bool maximize(T1 &a, T2 b){if (a < b) {a = b; return true;} return false;}
using namespace std;
struct node
{
node *child[26];
int num,maxh;
node()
{
memset(child,0,sizeof child);
num=maxh=0;
}
};
node *root=new node();
void add(string s)
{
node *p=root;
for(char c:s)
{
if(p->child[c-'a']==0) p->child[c-'a']=new node();
p=p->child[c-'a'];
}
p->num++;
}
string f;///ans :v
void dfs(node *u)
{
fo(i,0,25)
{
if(u->child[i])
{
dfs(u->child[i]);
u->maxh=max(u->maxh,u->child[i]->maxh+1);
}
}
}
void dfs_ans(node *u)
{
if(u->num) fo(i,1,u->num) f+='P';
int maxx=-1;
fo(i,0,25)
{
if(u->child[i])
{
if(maxx==-1) maxx=i;
else if(u->child[maxx]->maxh<u->child[i]->maxh) maxx=i;
}
}
fo(i,0,25)
{
if(i==maxx) continue;
if(u->child[i])
{
f+=(i+'a');
dfs_ans(u->child[i]);
f+='-';
}
}
if(maxx!=-1)
{
f+=(maxx+'a');
dfs_ans(u->child[maxx]);
f+='-';
}
}
void process()
{
int n;
cin>>n;
fo(i,1,n)
{
string s;
cin>>s;
add(s);
}
dfs(root);
dfs_ans(root);
while(f.back()=='-') f.pop_back();
cout<<f.size()<<"\n";
for(char c:f) cout<<c<<"\n";
}
main()
{
ios_base::sync_with_stdio(false);cin.tie(NULL);
process();
}
Compilation message (stderr)
printer.cpp:102:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
102 | main()
| ^~~~
# | 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... |