#include <string> #include <iostream> #include <algorithm> using namespace std; int main(int argc, char const *argv[]) { int n; int one_count = 0; cin >> n; for (int i = 0; i < 8 * n; i++) { char c; cin >> c; if (c == '1') { one_count++; } } int zero_count = 8 * n - one_count; int a_count = 0; int c_count = 0; int g_count = 0; int o_count = 0; while (one_count > 0 && zero_count > 0) { /* a - 3, c - 4, g - 5, o - 6, */ long long all = (zero_count + one_count); long long one_count_8 = 8L * one_count; if (one_count_8 < 3L * all || one_count_8 > 6L * all) { break; } else if (one_count_8 >= 3L * all && one_count_8 < 4L * all) { a_count++; one_count -= 3; zero_count -= 5; } else if (one_count_8 >= 4L * all && one_count_8 < 5L * all) { c_count++; one_count -= 4; zero_count -= 4; } else if (one_count_8 >= 5L * all && one_count_8 < 6L * all) { g_count++; one_count -= 5; zero_count -= 3; } else { o_count++; one_count -= 6; zero_count -= 2; } } if (one_count > 0 || zero_count > 0) { std::cout << "NIE" << endl; } else { std::cout << string(a_count, 'a') << string(c_count, 'c') << string(g_count, 'g') << string(o_count, 'o') << endl; } return 0; }
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 69 70 71 72 73 | #include <string> #include <iostream> #include <algorithm> using namespace std; int main(int argc, char const *argv[]) { int n; int one_count = 0; cin >> n; for (int i = 0; i < 8 * n; i++) { char c; cin >> c; if (c == '1') { one_count++; } } int zero_count = 8 * n - one_count; int a_count = 0; int c_count = 0; int g_count = 0; int o_count = 0; while (one_count > 0 && zero_count > 0) { /* a - 3, c - 4, g - 5, o - 6, */ long long all = (zero_count + one_count); long long one_count_8 = 8L * one_count; if (one_count_8 < 3L * all || one_count_8 > 6L * all) { break; } else if (one_count_8 >= 3L * all && one_count_8 < 4L * all) { a_count++; one_count -= 3; zero_count -= 5; } else if (one_count_8 >= 4L * all && one_count_8 < 5L * all) { c_count++; one_count -= 4; zero_count -= 4; } else if (one_count_8 >= 5L * all && one_count_8 < 6L * all) { g_count++; one_count -= 5; zero_count -= 3; } else { o_count++; one_count -= 6; zero_count -= 2; } } if (one_count > 0 || zero_count > 0) { std::cout << "NIE" << endl; } else { std::cout << string(a_count, 'a') << string(c_count, 'c') << string(g_count, 'g') << string(o_count, 'o') << endl; } return 0; } |