# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
222914 | miello | Type Printer (IOI08_printer) | C++11 | 258 ms | 101224 KiB |
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;
const int MXN = 25005;
struct node {
bool finalword = false , need = false;
struct node* alpha[26];
node() {
for(int i = 0 ; i < 26 ; i++){
this->alpha[i] = nullptr;
}
}
};
vector<char> ans;
vector<string> s;
void insert(node* now , string s , int idx , bool fin) {
if(s.size() == idx) {
now->need = true;
return;
}
if(now->alpha[s[idx] - 'a'] == nullptr){
now->alpha[s[idx] - 'a'] = new node();
now->alpha[s[idx] - 'a']->finalword = fin;
}
insert(now->alpha[s[idx] - 'a'] , s , idx + 1 , fin);
}
void dfs(node* now) {
if(now->need) {
ans.push_back('P');
}
for(int i = 0 ; i < 26 ; i++){
if(now->alpha[i] != nullptr) {
if(!now->alpha[i]->finalword) {
ans.push_back('a' + i);
dfs(now->alpha[i]);
ans.push_back('-');
}
}
}
for(int i = 0 ; i < 26 ; i++){
if(now->alpha[i] != nullptr) {
if(now->alpha[i]->finalword) {
ans.push_back('a' + i);
dfs(now->alpha[i]);
}
}
}
}
int main(){
int n;
scanf("%d" ,&n);
for(int i = 0 ; i < n ; i++){
string a;
cin >> a;
s.push_back(a);
}
sort(s.begin() , s.end() , [](string a , string b) {
return a.size() > b.size();
});
node* root = new node();
for(int i = 0 ; i < n ; i++) {
if(i == 0) {
insert(root, s[i], 0, true);
continue;
}
insert(root, s[i], 0, false);
}
// printf("%d" ,root->alpha['p' - 'a']->alpha['r' - 'a']->finalword);
dfs(root);
printf("%d\n", ans.size());
for(auto i : ans) {
printf("%c\n" ,i);
}
}
Compilation message (stderr)
# | 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... |