This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |