#include <iostream> #include <string> #include <vector> using namespace std; void add_letter(vector<char>& res, char letter, int count) { while (count > 0) { res.push_back(letter); count--; } } int main() { int n; string bits; cin >> n >> bits; int ones = 0; for (char bit : bits) { ones += (bit == '1'); } int a = -1, c = -1, k = -1, o = -1; int cko = ones - 3 * n; if (cko >= 0) { for (int try_c = 0; try_c <= min(n, cko); ++try_c) { int ko = cko - try_c; if (ko != 1) { int try_o, try_k; if (ko % 3 == 0) { try_o = ko / 3; try_k = 0; } else if (ko % 3 == 1) { try_k = 2; try_o = (ko - 4) / 3; } else { try_k = 1; try_o = (ko - 2) / 3; } int try_a = n - try_c - try_k - try_o; if (try_a >= 0 && try_k >= 0 && try_o >= o && n == try_a + try_c + try_k + try_o) { a = try_a; c = try_c; k = try_k; o = try_o; break; } } } } if (a == -1) { cout << "NIE" << '\n'; return 0; } vector<char> result_vector; add_letter(result_vector, 'a', a); add_letter(result_vector, 'c', c); add_letter(result_vector, 'k', k); add_letter(result_vector, 'o', o); string result(result_vector.begin(), result_vector.end()); cout << result << '\n'; 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 | #include <iostream> #include <string> #include <vector> using namespace std; void add_letter(vector<char>& res, char letter, int count) { while (count > 0) { res.push_back(letter); count--; } } int main() { int n; string bits; cin >> n >> bits; int ones = 0; for (char bit : bits) { ones += (bit == '1'); } int a = -1, c = -1, k = -1, o = -1; int cko = ones - 3 * n; if (cko >= 0) { for (int try_c = 0; try_c <= min(n, cko); ++try_c) { int ko = cko - try_c; if (ko != 1) { int try_o, try_k; if (ko % 3 == 0) { try_o = ko / 3; try_k = 0; } else if (ko % 3 == 1) { try_k = 2; try_o = (ko - 4) / 3; } else { try_k = 1; try_o = (ko - 2) / 3; } int try_a = n - try_c - try_k - try_o; if (try_a >= 0 && try_k >= 0 && try_o >= o && n == try_a + try_c + try_k + try_o) { a = try_a; c = try_c; k = try_k; o = try_o; break; } } } } if (a == -1) { cout << "NIE" << '\n'; return 0; } vector<char> result_vector; add_letter(result_vector, 'a', a); add_letter(result_vector, 'c', c); add_letter(result_vector, 'k', k); add_letter(result_vector, 'o', o); string result(result_vector.begin(), result_vector.end()); cout << result << '\n'; return 0; } |