#include <iostream> #include <string> #include <unordered_map> std::string solve(int n, int count_1) { std::unordered_map<int, char> signs; signs[1] = 'a'; signs[2] = 'c'; signs[3] = 'g'; signs[4] = 'o'; std::string result(n, 'a'); for (int i = 0; i<n; ++i) { if (count_1 == n-i) break; if (count_1-4 >= n-i-1) { count_1 -=4; result[i] = signs[4]; } else { int diff = count_1 - (n-i-1); result[i] = signs[diff]; count_1 -= diff; } } return result; } int main() { int n; std::cin >> n; int count_1 = 0; for (int i = 0; i<8*n; ++i) { char tmp; std::cin >> tmp; count_1 += (tmp-'0'); } count_1 -= 2*n; if (count_1<n || count_1>4*n) std::cout << "NIE" << std::endl; else std::cout << solve(n, count_1) << std::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 | #include <iostream> #include <string> #include <unordered_map> std::string solve(int n, int count_1) { std::unordered_map<int, char> signs; signs[1] = 'a'; signs[2] = 'c'; signs[3] = 'g'; signs[4] = 'o'; std::string result(n, 'a'); for (int i = 0; i<n; ++i) { if (count_1 == n-i) break; if (count_1-4 >= n-i-1) { count_1 -=4; result[i] = signs[4]; } else { int diff = count_1 - (n-i-1); result[i] = signs[diff]; count_1 -= diff; } } return result; } int main() { int n; std::cin >> n; int count_1 = 0; for (int i = 0; i<8*n; ++i) { char tmp; std::cin >> tmp; count_1 += (tmp-'0'); } count_1 -= 2*n; if (count_1<n || count_1>4*n) std::cout << "NIE" << std::endl; else std::cout << solve(n, count_1) << std::endl; return 0; } |