#include <bits/stdc++.h>
using namespace std;
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int,int>
#define vpii vector<pii>
#define vvpii vector<vpii>
#define vb vector<bool>
#define vc vector<char>
#define vvc vector<vc>
#define vvb vector<vb>z
#define si set<int>
#define mii map<int,int>
const int MOD=1e9+7;
const int N=2e5+1;
struct Nodo{
map<char,Nodo*>childs;
bool end=false;
int depth=1;
};
void add(Nodo* Trie,string& word,int index=0){
if((int)word.size()==index){
Trie->end=true;
return;
}
char character=word[index];
if(Trie->childs.find(character)==Trie->childs.end()){
Trie->childs[character]=new Nodo;
}
add(Trie->childs[character],word,index+1);
Trie->depth=max(Trie->depth,Trie->childs[character]->depth+1);
}
vc sol;
int ned=0;
void solve(Nodo* Trie){
char last='P';
if(ned and Trie->end)sol.push_back('P'),ned--;
int ma=0;
for(char e='a';e<='z';e++){
if(Trie->childs.find(e)!=Trie->childs.end()){
if(Trie->childs[e]->depth>ma){
ma=Trie->childs[e]->depth;
last=e;
}
}
}
//cerr<<Trie->depth<<" "<<last<<endl;
for(char e='a';e<='z';e++){
if(e==last)continue;
if(Trie->childs.find(e)!=Trie->childs.end()){
//cerr<<char(e)<<endl;
if(ned)sol.push_back(e);
solve(Trie->childs[e]);
if(ned)sol.push_back('-');
}
}
if(last=='P')return;
if(ned)sol.push_back(last);
solve(Trie->childs[last]);
if(ned)sol.push_back('-');
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin>>n;
ned=n;
Nodo* Root=new Nodo;
for(int i=0;i<n;i++){
string s;
cin>>s;
add(Root,s);
}
solve(Root);
cout<<sol.size()<<"\n";
for(char e : sol)cout<<e<<"\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1108 KB |
Output is correct |
2 |
Correct |
4 ms |
1492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
3532 KB |
Output is correct |
2 |
Correct |
24 ms |
7528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
8708 KB |
Output is correct |
2 |
Correct |
11 ms |
2132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
74 ms |
21396 KB |
Output is correct |
2 |
Correct |
169 ms |
49160 KB |
Output is correct |
3 |
Correct |
93 ms |
25540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
75 ms |
16792 KB |
Output is correct |
2 |
Correct |
194 ms |
58428 KB |
Output is correct |
3 |
Correct |
104 ms |
29004 KB |
Output is correct |
4 |
Correct |
168 ms |
55124 KB |
Output is correct |