#include<bits/stdc++.h>
using namespace std;
struct Node{
Node* c[26];
int maxdepth=0;
Node(){
for(int i=0;i<26;i++) c[i]=nullptr;
}
};
Node* root=new Node();
void insert(string s){
Node* node=root;
int n=s.length();
for(int i=0;i<n;i++){
if(node->c[s[i]-'a']==nullptr) node->c[s[i]-'a'] = new Node();
node=node->c[s[i]-'a'];
node->maxdepth=max(node->maxdepth,n);
}
}
int k;
vector<char> op;
void dfs(Node* node){
vector<pair<int,int>> v;
for(int i=0;i<26;i++){
if(node->c[i]){
v.push_back({node->c[i]->maxdepth,i});
}
}
if(v.empty()){
k--;
op.push_back('P');
return;
}
sort(v.begin(),v.end());
for(auto x:v){
op.push_back(char('a'+x.second));
dfs(node->c[x.second]);
if(k!=0){
op.push_back('-');
}
}
}
signed main(){
int n;
cin>>n;
k=n;
vector<string> v(n);
for(int i=0;i<n;i++){
cin>>v[i];
insert(v[i]);
}
dfs(root);
cout<<op.size()<<endl;
for(auto x:op){
cout<<x<<endl;
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
344 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
didn't print every word |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
344 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
15 ms |
1880 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
56 ms |
6160 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
140 ms |
15012 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
398 ms |
37712 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
296 ms |
29596 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |