#include <iostream> #include <cstdio> #include <unordered_map> int main() { int n; std::cin >> n; // Smallest number of 1 bits in an ASCI latin letter is 3 (e.g for 'a') const int MIN_NUM_ONES = 3 * n; // Highest number of 1 bits in an ASCI latin letter is 6 (e.g for 'o') const int MAX_NUM_ONES = 6 * n; // Mapping for num ones per byte to example char const std::unordered_map<int, char> NUM_ONES_PER_BYTE_TO_CHAR{ {3, 'a'}, {4, 'c'}, {5, 'g'}, {6, 'o'}, {7, '\0'} }; int numOnes = 0; // Go through the bits int i = 0; for(i = 0 ; i < 8 * n ; ++i) { char bit; std::cin >> bit; numOnes += (bit == '1'); } // Incorrect number of 1 bits if(numOnes < MIN_NUM_ONES || MAX_NUM_ONES < numOnes) { std::cout << "NIE" << std::endl; } else { auto meanOnesPerByte = numOnes / n; auto reminder = numOnes - n * meanOnesPerByte; // Print (n - reminder) of the mean ones-per-byte chars auto exampleCharIt = NUM_ONES_PER_BYTE_TO_CHAR.find(meanOnesPerByte); for(int i = 0; i < n - reminder; ++i) { std::cout << exampleCharIt->second; } // Print the rest with mean+1 ones-per-byte chars exampleCharIt = NUM_ONES_PER_BYTE_TO_CHAR.find(meanOnesPerByte + 1); for(int i = 0; i < reminder; ++i) { std::cout << exampleCharIt->second; } std::cout << 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 | #include <iostream> #include <cstdio> #include <unordered_map> int main() { int n; std::cin >> n; // Smallest number of 1 bits in an ASCI latin letter is 3 (e.g for 'a') const int MIN_NUM_ONES = 3 * n; // Highest number of 1 bits in an ASCI latin letter is 6 (e.g for 'o') const int MAX_NUM_ONES = 6 * n; // Mapping for num ones per byte to example char const std::unordered_map<int, char> NUM_ONES_PER_BYTE_TO_CHAR{ {3, 'a'}, {4, 'c'}, {5, 'g'}, {6, 'o'}, {7, '\0'} }; int numOnes = 0; // Go through the bits int i = 0; for(i = 0 ; i < 8 * n ; ++i) { char bit; std::cin >> bit; numOnes += (bit == '1'); } // Incorrect number of 1 bits if(numOnes < MIN_NUM_ONES || MAX_NUM_ONES < numOnes) { std::cout << "NIE" << std::endl; } else { auto meanOnesPerByte = numOnes / n; auto reminder = numOnes - n * meanOnesPerByte; // Print (n - reminder) of the mean ones-per-byte chars auto exampleCharIt = NUM_ONES_PER_BYTE_TO_CHAR.find(meanOnesPerByte); for(int i = 0; i < n - reminder; ++i) { std::cout << exampleCharIt->second; } // Print the rest with mean+1 ones-per-byte chars exampleCharIt = NUM_ONES_PER_BYTE_TO_CHAR.find(meanOnesPerByte + 1); for(int i = 0; i < reminder; ++i) { std::cout << exampleCharIt->second; } std::cout << std::endl; } return 0; } |