#include <iostream> #include <cmath> using namespace std; int ascii[27][2]; int closest_ratio(double r) { int index = 1; for (int i = 1; i <= 26; ++i) { // cout << fabs(r - (double)ascii[i][0] / ascii[i][1]) << " "; if (fabs(r - (double)ascii[i][0] / ascii[i][1]) < fabs(r - (double)ascii[index][0] / ascii[index][1])) { // cout << (char) (96 + i); index = i; } // cout << "\n"; } return index; } int main() { int n; cin >> n; string tab; cin >> tab; for (int i = 0; i < 26; ++i) { char x = 97 + i; int zero = 0; int one = 0; // cout << x << ": "; for (int b = 0; b < 8; ++b) { if (x & (1 << b)) one++; else zero++; } /* cout << "zeros: " << zero << " "; cout << "ones: " << one << " "; cout << "r: " << (double) zero / one << "\n"; */ ascii[i + 1][0] = zero; ascii[i + 1][1] = one; } int zeros = 0, ones = 0; for (int i = 0; i < 8 * n; ++i) { if (tab[i] == '0') zeros++; } ones = 8 * n - zeros; // cout << zeros << " " << ones << " " << (double) zeros / ones << "\n"; if (ones < 3 * n || ones > 6 * n || zeros < 2 * n || zeros > 5 * n) { cout << "NIE\n"; return 0; } char res[n + 1]; double ratio = (double) zeros / ones; int x = 0; while (ones || zeros) { int i = closest_ratio(ratio); zeros -= ascii[i][0]; ones -= ascii[i][1]; // cout << (char)(96 + i) << " " << (double) ascii[i][0] / ascii[i][1] << " "; // cout << zeros << " " << ones << "\n"; res[x++] = 96 + i; ratio = (double) zeros / ones; } res[x] = '\0'; cout << res << "\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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | #include <iostream> #include <cmath> using namespace std; int ascii[27][2]; int closest_ratio(double r) { int index = 1; for (int i = 1; i <= 26; ++i) { // cout << fabs(r - (double)ascii[i][0] / ascii[i][1]) << " "; if (fabs(r - (double)ascii[i][0] / ascii[i][1]) < fabs(r - (double)ascii[index][0] / ascii[index][1])) { // cout << (char) (96 + i); index = i; } // cout << "\n"; } return index; } int main() { int n; cin >> n; string tab; cin >> tab; for (int i = 0; i < 26; ++i) { char x = 97 + i; int zero = 0; int one = 0; // cout << x << ": "; for (int b = 0; b < 8; ++b) { if (x & (1 << b)) one++; else zero++; } /* cout << "zeros: " << zero << " "; cout << "ones: " << one << " "; cout << "r: " << (double) zero / one << "\n"; */ ascii[i + 1][0] = zero; ascii[i + 1][1] = one; } int zeros = 0, ones = 0; for (int i = 0; i < 8 * n; ++i) { if (tab[i] == '0') zeros++; } ones = 8 * n - zeros; // cout << zeros << " " << ones << " " << (double) zeros / ones << "\n"; if (ones < 3 * n || ones > 6 * n || zeros < 2 * n || zeros > 5 * n) { cout << "NIE\n"; return 0; } char res[n + 1]; double ratio = (double) zeros / ones; int x = 0; while (ones || zeros) { int i = closest_ratio(ratio); zeros -= ascii[i][0]; ones -= ascii[i][1]; // cout << (char)(96 + i) << " " << (double) ascii[i][0] / ascii[i][1] << " "; // cout << zeros << " " << ones << "\n"; res[x++] = 96 + i; ratio = (double) zeros / ones; } res[x] = '\0'; cout << res << "\n"; return 0; } |