#include <iostream> // std::cout #include <algorithm> // std::sort #include <vector> // std::vector #include <set> #include <cmath> #include <string> #include <map> #include <cassert> using namespace std; int n, ones; string input, output; map<int, char> character_mapping { {3,'a'}, {4,'c'}, {5,'g'}, {6,'o'} }; bool is_valid_state(int n, int k){ int lower_bound = k/6 + ((k%6)? 1 : 0); int upper_bound = k/3; return n >= lower_bound && n <= upper_bound; } int main() { scanf("%d\n", &n); getline(cin, input); for (auto c: input){ ones += (int) c - '0'; } for (int bytes=n, k=ones; bytes>0; bytes--){ if (!is_valid_state(bytes, k)){ cout << "NIE"; return 0; } if (bytes == 1){ assert(k >= 3 && k<=6); output.push_back(character_mapping[k]); continue; } // try to find next possible number of ones given bytes-1 int substraction; for (substraction = 3; substraction <= 6 && !is_valid_state(bytes-1, k-substraction); ++substraction); k-=substraction; output.push_back(character_mapping[substraction]); } cout << output; 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 | #include <iostream> // std::cout #include <algorithm> // std::sort #include <vector> // std::vector #include <set> #include <cmath> #include <string> #include <map> #include <cassert> using namespace std; int n, ones; string input, output; map<int, char> character_mapping { {3,'a'}, {4,'c'}, {5,'g'}, {6,'o'} }; bool is_valid_state(int n, int k){ int lower_bound = k/6 + ((k%6)? 1 : 0); int upper_bound = k/3; return n >= lower_bound && n <= upper_bound; } int main() { scanf("%d\n", &n); getline(cin, input); for (auto c: input){ ones += (int) c - '0'; } for (int bytes=n, k=ones; bytes>0; bytes--){ if (!is_valid_state(bytes, k)){ cout << "NIE"; return 0; } if (bytes == 1){ assert(k >= 3 && k<=6); output.push_back(character_mapping[k]); continue; } // try to find next possible number of ones given bytes-1 int substraction; for (substraction = 3; substraction <= 6 && !is_valid_state(bytes-1, k-substraction); ++substraction); k-=substraction; output.push_back(character_mapping[substraction]); } cout << output; return 0; } |