#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,height;
node *child[26];
vector<int> v;
};
node *root,*srt;
int n;
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);
}
}
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]){
cout<<(char)('a'+i)<<"\n";
dfs2(now->child[i]);
}
if(now->leaf) cout<<"P\n",n--;
if(n) cout<<"-\n";
}
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);
//endscreen();
return 0;
}
Compilation message
printer.cpp: In function 'void insert(std::__cxx11::string)':
printer.cpp:62:25: warning: array subscript has type 'char' [-Wchar-subscripts]
if(!now->child[c]) now->child[c]=new node();
^
printer.cpp:62:40: warning: array subscript has type 'char' [-Wchar-subscripts]
if(!now->child[c]) now->child[c]=new node();
^
printer.cpp:63:25: warning: array subscript has type 'char' [-Wchar-subscripts]
now=now->child[c];
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
384 KB |
Expected integer, but "t" found |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
384 KB |
Expected integer, but "n" found |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
384 KB |
Expected integer, but "x" found |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
384 KB |
Expected integer, but "x" found |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
640 KB |
Expected integer, but "n" found |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
12 ms |
3840 KB |
Expected integer, but "n" found |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
36 ms |
13280 KB |
Expected integer, but "n" found |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
82 ms |
33400 KB |
Expected integer, but "n" found |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
200 ms |
83832 KB |
Expected integer, but "n" found |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
173 ms |
65272 KB |
Expected integer, but "n" found |
2 |
Halted |
0 ms |
0 KB |
- |