#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){
int best=-1;
int mx=-1;
for(int i=0;i<26;i++){
if(node->c[i]!=nullptr){
if(node->c[i]->maxdepth>mx){
mx=node->c[i]->maxdepth;
best=i;
}
}
}
if(node->leaf){
k--;
op.push_back('P');
}
for(int j=0;j<26;j++){
if(node->c[j]){
if(j==best) continue;
op.push_back(char('a'+j));
dfs(node->c[j]);
if(k!=0)
op.push_back('-');
}
}
if(best!=-1){
op.push_back(char(best+'a'));
dfs(node->c[best]);
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 |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
348 KB |
Output is correct |
2 |
Correct |
8 ms |
1116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
1968 KB |
Output is correct |
2 |
Correct |
19 ms |
2412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
62 ms |
6128 KB |
Output is correct |
2 |
Correct |
146 ms |
12772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
160 ms |
14964 KB |
Output is correct |
2 |
Correct |
47 ms |
3876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
413 ms |
37256 KB |
Output is correct |
2 |
Correct |
900 ms |
85020 KB |
Output is correct |
3 |
Correct |
458 ms |
44600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
305 ms |
29388 KB |
Output is correct |
2 |
Execution timed out |
1018 ms |
101068 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |