#include<bits/stdc++.h>
using namespace std;
struct Node{
Node* c[26];
int maxdepth=0;
bool leaf=false;
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);
}
node->leaf=true;
}
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]!=nullptr){
v.push_back({node->c[i]->maxdepth,i});
}
}
if(v.empty()){
k--;
op.push_back('P');
return;
}
if(node->leaf){
k--;
op.push_back('P');
}
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;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
344 KB |
Output is correct |
2 |
Correct |
8 ms |
1116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
1884 KB |
Output is correct |
2 |
Correct |
20 ms |
2392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
55 ms |
5980 KB |
Output is correct |
2 |
Correct |
119 ms |
12808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
148 ms |
14956 KB |
Output is correct |
2 |
Correct |
44 ms |
3928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
405 ms |
37340 KB |
Output is correct |
2 |
Correct |
811 ms |
85448 KB |
Output is correct |
3 |
Correct |
442 ms |
45000 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
314 ms |
29384 KB |
Output is correct |
2 |
Execution timed out |
1028 ms |
101652 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |