#include <bits/stdc++.h> using namespace std; int n; int zeros = 0; int ones = 0; string s; string ans = ""; int difference() { return abs(zeros - ones); } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cin >> n >> s; for (auto c : s) { c == '0' ? zeros++ : ones++; } if (3 * zeros < ones || 3 * zeros > 5 * ones) { cout << "NIE"; return 0; } while (zeros > ones) { ans += 'p'; zeros -= 5; ones -= 3; } while (zeros < ones) { if (difference() >= 4) { ans += 'o'; zeros -= 2; ones -= 6; } else { ans += 'n'; zeros -= 3; ones -= 5; } } while (zeros && ones) { ans += 'q'; zeros -= 4; ones -= 4; } cout << ans; 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 | #include <bits/stdc++.h> using namespace std; int n; int zeros = 0; int ones = 0; string s; string ans = ""; int difference() { return abs(zeros - ones); } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cin >> n >> s; for (auto c : s) { c == '0' ? zeros++ : ones++; } if (3 * zeros < ones || 3 * zeros > 5 * ones) { cout << "NIE"; return 0; } while (zeros > ones) { ans += 'p'; zeros -= 5; ones -= 3; } while (zeros < ones) { if (difference() >= 4) { ans += 'o'; zeros -= 2; ones -= 6; } else { ans += 'n'; zeros -= 3; ones -= 5; } } while (zeros && ones) { ans += 'q'; zeros -= 4; ones -= 4; } cout << ans; return 0; } |