#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 |
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 |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
256 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 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
512 KB |
Output is correct |
2 |
Correct |
6 ms |
1408 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
2176 KB |
Output is correct |
2 |
Correct |
8 ms |
2688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
7168 KB |
Output is correct |
2 |
Correct |
27 ms |
15096 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
17652 KB |
Output is correct |
2 |
Correct |
13 ms |
4096 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
72 ms |
44144 KB |
Output is correct |
2 |
Correct |
145 ms |
101096 KB |
Output is correct |
3 |
Correct |
85 ms |
52084 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
70 ms |
34420 KB |
Output is correct |
2 |
Correct |
178 ms |
120296 KB |
Output is correct |
3 |
Correct |
107 ms |
59120 KB |
Output is correct |
4 |
Correct |
181 ms |
113512 KB |
Output is correct |