#include <cstdio> #include <stdio.h> #include <iostream> #include <string> #include <stdint.h> #include <unordered_set> #include <algorithm> using namespace std; static int32_t iN, iCountOfZeros, iCountOfOnes, iMinNumOfOnes, iMaxNumOfOnes; static char aCharForNumberOfOnes[64]; static string sInput; static string sOutput; static inline int CountOnes(char c) { int iCnt = 0; while (c != 0) { c &= (c - 1); iCnt++; } return iCnt; } static void ReadData() { cin >> iN >> sInput; } static void Init() { iCountOfZeros = 0; iCountOfOnes = 0; int32_t iBitCnt = iN << 3; for (int32_t i = 0; i < iBitCnt; ++i) { if (sInput[i] == '1') iCountOfOnes++; else iCountOfZeros++; } } static void InitTable() { for (char c = 'z'; c >= 'a'; --c) { int iNumOfOnes = CountOnes(c); aCharForNumberOfOnes[iNumOfOnes] = c; if (c == 'z') { iMinNumOfOnes = iNumOfOnes; iMaxNumOfOnes = iNumOfOnes; } else { iMinNumOfOnes = min(iNumOfOnes, iMinNumOfOnes); iMaxNumOfOnes = max(iNumOfOnes, iMaxNumOfOnes); } } } static void Solve() { int iAdditionalOnesCnt = iCountOfOnes - iMinNumOfOnes * iN; for (int i = 0; i < iN; ++i) { if (iAdditionalOnesCnt == 0) { sOutput.append(&aCharForNumberOfOnes[iMinNumOfOnes], 1); continue; } if (iAdditionalOnesCnt >= iMaxNumOfOnes - iMinNumOfOnes) { sOutput.append(&aCharForNumberOfOnes[iMaxNumOfOnes], 1); iAdditionalOnesCnt -= (iMaxNumOfOnes - iMinNumOfOnes); continue; } sOutput.append(&aCharForNumberOfOnes[iMinNumOfOnes + iAdditionalOnesCnt], 1); iAdditionalOnesCnt = 0; } } int main(int argc, char * argv[]) { ReadData(); Init(); InitTable(); if (iCountOfOnes > iMaxNumOfOnes * iN || iCountOfOnes < iMinNumOfOnes * iN) { cout << "NIE"; return 0; } Solve(); cout << sOutput; 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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | #include <cstdio> #include <stdio.h> #include <iostream> #include <string> #include <stdint.h> #include <unordered_set> #include <algorithm> using namespace std; static int32_t iN, iCountOfZeros, iCountOfOnes, iMinNumOfOnes, iMaxNumOfOnes; static char aCharForNumberOfOnes[64]; static string sInput; static string sOutput; static inline int CountOnes(char c) { int iCnt = 0; while (c != 0) { c &= (c - 1); iCnt++; } return iCnt; } static void ReadData() { cin >> iN >> sInput; } static void Init() { iCountOfZeros = 0; iCountOfOnes = 0; int32_t iBitCnt = iN << 3; for (int32_t i = 0; i < iBitCnt; ++i) { if (sInput[i] == '1') iCountOfOnes++; else iCountOfZeros++; } } static void InitTable() { for (char c = 'z'; c >= 'a'; --c) { int iNumOfOnes = CountOnes(c); aCharForNumberOfOnes[iNumOfOnes] = c; if (c == 'z') { iMinNumOfOnes = iNumOfOnes; iMaxNumOfOnes = iNumOfOnes; } else { iMinNumOfOnes = min(iNumOfOnes, iMinNumOfOnes); iMaxNumOfOnes = max(iNumOfOnes, iMaxNumOfOnes); } } } static void Solve() { int iAdditionalOnesCnt = iCountOfOnes - iMinNumOfOnes * iN; for (int i = 0; i < iN; ++i) { if (iAdditionalOnesCnt == 0) { sOutput.append(&aCharForNumberOfOnes[iMinNumOfOnes], 1); continue; } if (iAdditionalOnesCnt >= iMaxNumOfOnes - iMinNumOfOnes) { sOutput.append(&aCharForNumberOfOnes[iMaxNumOfOnes], 1); iAdditionalOnesCnt -= (iMaxNumOfOnes - iMinNumOfOnes); continue; } sOutput.append(&aCharForNumberOfOnes[iMinNumOfOnes + iAdditionalOnesCnt], 1); iAdditionalOnesCnt = 0; } } int main(int argc, char * argv[]) { ReadData(); Init(); InitTable(); if (iCountOfOnes > iMaxNumOfOnes * iN || iCountOfOnes < iMinNumOfOnes * iN) { cout << "NIE"; return 0; } Solve(); cout << sOutput; return 0; } |