1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <optional>
#include <string>
#include <vector>

void solve_dataset();
std::vector<int> get_bit_counts(std::istream& stream);
std::optional<std::string> recovered_text(std::vector<int> bit_counts);

int main() {
    std::ios_base::sync_with_stdio(false);
    solve_dataset();
    return 0;
}

void solve_dataset() {
    int _letter_count;
    std::cin >> _letter_count;
    const auto bit_counts = get_bit_counts(std::cin);
    const auto text = recovered_text(bit_counts);
    if(text.has_value())
        std::cout << text.value();
    else
        std::cout << "NIE";
    std::cout << std::endl;
}

std::vector<int> get_bit_counts(std::istream& stream) {
    std::vector<int> counts(2, 0);
    std::string bits;
    stream >> bits;
    for(const char bit : bits)
        counts[bit - '0']++;
    return counts;
}

std::optional<std::string> recovered_text(std::vector<int> bit_counts) {
    const int total_bits = bit_counts[0] + bit_counts[1];
    const int message_length = total_bits / 8;
    bit_counts[0] -= message_length;
    bit_counts[1] -= 2 * message_length;
    if(bit_counts[0] < 0 || bit_counts[1] < 0)
        return std::nullopt;
    if(bit_counts[1] > 4 * bit_counts[0] || bit_counts[0] > 4 * bit_counts[1])
        return std::nullopt;
    std::string message;
    message.reserve(message_length);
    for(int _ = 0; _ < message_length; _++) {
        if(2 * bit_counts[0] > 3 * bit_counts[1]) {
            bit_counts[0] -= 4;
            bit_counts[1] -= 1;
            message.push_back('a');
        } else if(bit_counts[0] > bit_counts[1]) {
            bit_counts[0] -= 3;
            bit_counts[1] -= 2;
            message.push_back('c');
        } else if(2 * bit_counts[1] > 3 * bit_counts[0]) {
            bit_counts[0] -= 1;
            bit_counts[1] -= 4;
            message.push_back('o');
        } else {
            bit_counts[0] -= 2;
            bit_counts[1] -= 3;
            message.push_back('g');
        }
    }
    return message;
}