# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
120423 | miello | Type Printer (IOI08_printer) | C++11 | 89 ms | 36472 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>
#define ii pair<int,int>
using namespace std;
struct node{
struct node *alpha[26];
bool endofword , el;
};
int num[26];
vector<ii> v;
queue<char> q;
int word , n;
void backtrack(node *h , int now){
q.push('a' + now);
if(h->el){
q.push('P');
word++;
}
for(int i = 0 ; i < 26 ; i++){
if(h->alpha[i] != NULL){
backtrack(h->alpha[i] , i);
}
}
if(word != n)q.push('-');
}
node* getnew(){
node *x = new node;
x->endofword = 0;
x->el = 0;
for(int i = 0 ; i < 26 ; i++){
x->alpha[i] = NULL;
}
return x;
}
int getsz(node *p){
int sz = 1;
if(p->endofword){
return 1;
}
for(int i = 0 ; i < 26 ; i++){
if(p->alpha[i] != NULL){
sz += getsz(p->alpha[i]);
}
}
return sz;
}
int main(){
scanf("%d",&n);
node *head = getnew();
for(int i = 0 ; i < n ; i++){
string s;
cin >> s;
int l = s.size();
node *x;
x = head;
for(int j = 0 ; j < l ; j++){
if(x->alpha[s[j] - 'a'] == NULL){
x->alpha[s[j] - 'a'] = getnew();
x->endofword = 0;
x->alpha[s[j] - 'a']->endofword = 1;
}
x = x->alpha[s[j] - 'a'];
}
x->el = 1;
}
for(int i = 0 ; i < 26 ; i++){
if(head->alpha[i] != NULL){
v.emplace_back(getsz(head->alpha[i]) , i);
}
}
sort(v.begin() , v.end());
for(auto i : v){
int u = i.second;
backtrack(head->alpha[u] , u);
}
printf("%d\n",q.size());
while(q.size()){
printf("%c\n",q.front());
q.pop();
}
}
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... |