# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
958386 | mkupanovac | Type Printer (IOI08_printer) | C++14 | 158 ms | 108096 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 ll long long
#define PB push_back
#define X first
#define Y second
#define MP make_pair
#define pii pair<int, int>
const int MAXN = (1e5 + 10);
const int INF = (1e9 + 7);
using namespace std;
int n;
vector<string> v;
vector<char> sol;
struct cvor{
char slovo;
int br;
int slispod = 0;
cvor* djeca[26];
cvor(char slovo2){
slovo = slovo2;
br = 0;
slispod = 0;
for (int i = 0; i < 26; i++){
djeca[i] = nullptr;
}
}
};
cvor root('\0');
void dodaj(string s){
cvor* tr = &root;
for (auto x: s){
if (tr -> djeca[x - 'a'] == nullptr){
tr -> djeca[x - 'a'] = new cvor(x);
}
tr = tr -> djeca[x - 'a'];
tr -> br++;
}
}
int ispod(cvor* tr){
if (tr -> slispod)
return tr -> slispod;
int trisp = 0;
for (int i = 0; i < 26; i++){
if (tr -> djeca[i] != nullptr)
trisp++;
}
//cout << tr -> slovo << " " << trisp << "\n";
if (!trisp){
//cout << tr -> slovo << " " << 0 << "\n";
tr -> slispod = 0;
return 0;
}
int trzbr = 0;
for (int i = 0; i < 26; i++){
if (tr -> djeca[i] != nullptr){
int novi = 1 + ispod(tr -> djeca[i]);
trzbr = max(trzbr, novi);
//cout << tr -> slovo << " u " << tr -> djeca[i] -> slovo << " " << novi << " novi\n";
}
}
//cout << tr -> slovo << " " << trzbr << "\n";
tr -> slispod = trzbr;
return trzbr;
}
void solve(cvor* tr){
int uk = 0;
vector<pii> trv;
for (int i = 0; i < 26; i++){
if (tr -> djeca[i] != nullptr){
trv.PB(MP(tr -> djeca[i] -> slispod, i));
uk += tr -> djeca[i] -> br;
}
}
if (tr -> br - uk == 1){
sol.PB('P');
}
sort (trv.begin(), trv.end());
for (int i = 0; i < trv.size(); i++){
//cout << trv[i].X << " za " << char('a' + trv[i].Y) << " ";
//cout << trv.size() << " " << trv[i].Y << " i\n";
sol.PB('a' + trv[i].Y);
solve(tr -> djeca[trv[i].Y]);
sol.PB('-');
}
//cout << "\n";
/*if (!trv.size()){
sol.PB('P');
}*/
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 0; i < n; i++){
string s;
cin >> s;
v.PB(s);
dodaj(s);
}
ispod(&root);
//cout << ispod(&root) << " za root\n";
solve(&root);
int koliko = 0;
for (int i = sol.size() - 1; i >= 0; i--){
if (sol[i] == '-')
koliko++;
else
break;
}
cout << sol.size() - koliko << "\n";
for (int i = 0; i < sol.size() - koliko; i++){
cout << sol[i] << "\n";
}
return 0;
}
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... |