#include<bits/stdc++.h>
using namespace std;
struct Node{
Node *nxt[26];
int dep,times;
Node(){
for(int i=0;i<26;i++) nxt[i] = NULL;
dep = times = 0;
}
};
void Insert(Node *root,string str){
if(str.empty()){
root->times++;
return;
}
char c = str.back();
if(root->nxt[c-'a'] == NULL) root->nxt[c-'a'] = new Node;
str.pop_back();
Insert(root->nxt[c-'a'],str);
root->dep = max(root->dep,root->nxt[c-'a']->dep+1);
}
vector<char> res;
void Run(Node *root){
for(int i=0;i<root->times;i++) res.push_back('P');
for(int i=0;i<26;i++){
if(root->nxt[i] != NULL && root->nxt[i]->dep != root->dep-1){
res.push_back(i+'a');
Run(root->nxt[i]);
res.push_back('-');
}
}
for(int i=0;i<26;i++){
if(root->nxt[i] != NULL && root->nxt[i]->dep == root->dep-1){
res.push_back(i+'a');
Run(root->nxt[i]);
res.push_back('-');
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int N;
cin >> N;
Node *root = new Node();
for(int i=0;i<N;i++){
string str;
cin >> str;
reverse(str.begin(),str.end());
Insert(root,str);
}
Run(root);
while(res.back() == '-') res.pop_back();
cout << res.size() << '\n';
for(char c : res) cout << c << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
1100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1740 KB |
Output is correct |
2 |
Correct |
4 ms |
2252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
5964 KB |
Output is correct |
2 |
Correct |
18 ms |
12504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
14628 KB |
Output is correct |
2 |
Correct |
9 ms |
3404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
57 ms |
36412 KB |
Output is correct |
2 |
Correct |
128 ms |
83740 KB |
Output is correct |
3 |
Correct |
65 ms |
43228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
45 ms |
28468 KB |
Output is correct |
2 |
Correct |
158 ms |
99644 KB |
Output is correct |
3 |
Correct |
71 ms |
48952 KB |
Output is correct |
4 |
Correct |
120 ms |
94012 KB |
Output is correct |