#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[str.back()-'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){
if(root->dep == 0){
for(int i=0;i<root->times;i++) res.push_back('P');
return;
}
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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
204 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Incorrect |
1 ms |
204 KB |
didn't print every word |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
1740 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
9 ms |
5932 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
23 ms |
14668 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
55 ms |
36388 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
45 ms |
28400 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |