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 <cstdio>


double get_ratio(int count0, int count1) {

    if (count0 > count1) {
        return (double) count0/count1;
    }

    return (double) count1/count0;
}


int main(int argc, char const *argv[]) {
    int n;
    char* bits;
    char mapping[5][5] = {
        {'0','0','0','0','0'},
        {'0','0','0','0','o'},
        {'0','0','0','g','0'},
        {'0','0','c','0','0'},
        {'0','a','0','0','0'},
    };
    int count[2] = {0, 0};
    scanf("%d", &n);

    bits = new char[8*n+100];

    scanf("%s", bits);

    for (int i = 0; i < n*8; i++) {
        count[bits[i] - '0']++;
    }

    count[0] -= n;
    count[1] -= n*2;

    double ratio = get_ratio(count[0], count[1]);

    if (ratio > 4.0 || count[0] < 0 || count[1] < 0) {
        puts("NIE");

        return 0;
    }

    while (count[0] && count[1]) {
        char to_delete = count[0] > count[1] ? 0 : 1;

        while (count[0] + count[1] > 5 && count[to_delete] >= count[-to_delete + 1]) {
            count[to_delete] -= 4;
            count[-to_delete + 1] -= 1;

            if (to_delete == 0) {
                printf("%c", mapping[4][1]);
            } else {
                printf("%c", mapping[1][4]);
            }
        }

        if (count[0] + count[1] == 5) {
            printf("%c", mapping[count[0]][count[1]]);
            break;
        }
    }
    puts("");

    return 0;
}