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>
#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
const int MAXN=25001;
int n;
vector<char> ans;
class node{
public:
bool w=0,p=0;
char id;
vector<node*> child;
node(){
child.resize(26,NULL);
};
};
node* root;
void insert(string x,bool p){
node *now=root;
for(auto c:x){
c-='a';
if(!now->child[c]) now->child[c]=new node();
now=now->child[c];
now->id=c+'a';
now->p=p;
}
now->w=1;
}
void dfs(node *root){
node *now=root;
if(now->w) ans.push_back('P');
node *rem=NULL;
for(auto nd:now->child)if(nd){
if(nd->p) rem=nd;
else{
ans.push_back(nd->id);
dfs(nd);
ans.push_back('-');
}
}
if(rem){
ans.push_back(rem->id);
dfs(rem);
}
}
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();
string mx;
for(int i=0;i<n;i++){
string x;
cin>>x;
insert(x,0);
if(x.size()>mx.size()) mx=x;
}
insert(mx,1);
dfs(root);
cout<<ans.size();
for(auto c:ans) cout<<"\n"<<c;
//endscreen();
return 0;
}
# | 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... |