#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx,avx2,fma")
using namespace std;
using namespace __gnu_pbds;
#define int long long
#define pii pair<int,int>
#define pqi priority_queue<int>
#define pb push_back()
#define INF 1000000000000000000
#define pi acos(-1)
#define deb(x) cout<<#x" = "<<x<<" "
#define ordered_set tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>
#define all(x) x.begin(),x.end()
const int modu=1e9+7;
#define ull unsigned long long
int bigmod(int n,int p){
if(p==0) return 1;
int x=bigmod(n,p/2)%modu;
x=(x*x)%modu;
if(p%2) return (x*n)%modu;
return x;
}
int modInverse(int a){
return bigmod(a,modu-2)%modu;
}
int add(int a,int b){
int ans=(a%modu + b%modu)%modu;
while(ans<0) ans+=modu;
return ans;
}
int mul(int a,int b){
int ans=(a%modu * b%modu)%modu;
while(ans<0) ans+=modu;
return ans;
}
// starts here
class node{
public:
bool leaf;
int height;
node *child[26];
vector<int> v;
};
node *root,*srt;
int n;
vector<char> ans;
void insert(string x){
node *now=root;
for(auto c:x){
c-='a';
if(!now->child[c]) now->child[c]=new node();
now=now->child[c];
}
now->leaf=true;
}
bool cmp(int a,int b){
int ha= (srt->child[a])? srt->child[a]->height:0;
int hb= (srt->child[b])? srt->child[b]->height:0;
return ha<hb;
}
void dfs1(node *now){
now->height=0;
for(auto nd:now->child){
if(nd){
dfs1(nd);
now->height=max(now->height,nd->height+1LL);
}
}
for(int i=0;i<26;i++) now->v.push_back(i);
srt=now;
sort(all(now->v),cmp);
}
void dfs2(node *now){
for(auto i:now->v)if(now->child[i]){
ans.push_back('a'+i);
dfs2(now->child[i]);
}
if(now->leaf) ans.push_back('P'),n--;
if(n) ans.push_back('-');
}
void dfs3(node *now){
deb(now->height)<<endl;
//for(auto i:now->v) cout<<i<<" ";
//cout<<"\n";
for(auto nd:now->child){
if(nd) dfs3(nd);
}
}
int32_t main(){
ios_base::sync_with_stdio(false);cin.tie();cout.tie();
//If you hack my code , You are gay;
cin>>n;
root=new node();
for(int i=0;i<n;i++){
string x;
cin>>x;
insert(x);
}
dfs1(root);
dfs2(root);
// dfs3(root);
//return 0;
cout<<ans.size()<<"\n";
for(auto i:ans) cout<<i<<"\n";
//endscreen();
return 0;
}
Compilation message
printer.cpp: In function 'void insert(std::__cxx11::string)':
printer.cpp:64:25: warning: array subscript has type 'char' [-Wchar-subscripts]
if(!now->child[c]) now->child[c]=new node();
^
printer.cpp:64:40: warning: array subscript has type 'char' [-Wchar-subscripts]
if(!now->child[c]) now->child[c]=new node();
^
printer.cpp:65:25: warning: array subscript has type 'char' [-Wchar-subscripts]
now=now->child[c];
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
640 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
12 ms |
3840 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
36 ms |
13304 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
86 ms |
33656 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
217 ms |
84088 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
162 ms |
65520 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |