#include <bits/stdc++.h> #define FOR(i, n) for(int i = 0; i < (n); ++i) #define REP(i, a, b) for(int i = (a); i < (b); ++i) #define TRAV(i, a) for(auto & i : (a)) #define SZ(x) ((int)(x).size()) #define X first #define Y second #define PR std::pair #define MP std::make_pair typedef long long ll; typedef std::pair<int, int> PII; typedef std::vector<int> VI; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(0); int n; std::cin >> n; std::string s; std::cin >> s; VI cnt(2); TRAV(i, s) cnt[i-'0']++; int balance = cnt[1] - cnt[0]; std::map<int, char> map; FOR(i, 26){ map[2 * __builtin_popcount('a' + i) - 8] = 'a' + i; } std::string ans; FOR(i, n){ int want = balance; if(want < -2) want = -2; if(want > 4) want = 4; assert(map.count(want)); ans += map[want]; balance -= want; } if(balance == 0) std::cout << ans << "\n"; else std::cout << "NIE\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 | #include <bits/stdc++.h> #define FOR(i, n) for(int i = 0; i < (n); ++i) #define REP(i, a, b) for(int i = (a); i < (b); ++i) #define TRAV(i, a) for(auto & i : (a)) #define SZ(x) ((int)(x).size()) #define X first #define Y second #define PR std::pair #define MP std::make_pair typedef long long ll; typedef std::pair<int, int> PII; typedef std::vector<int> VI; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(0); int n; std::cin >> n; std::string s; std::cin >> s; VI cnt(2); TRAV(i, s) cnt[i-'0']++; int balance = cnt[1] - cnt[0]; std::map<int, char> map; FOR(i, 26){ map[2 * __builtin_popcount('a' + i) - 8] = 'a' + i; } std::string ans; FOR(i, n){ int want = balance; if(want < -2) want = -2; if(want > 4) want = 4; assert(map.count(want)); ans += map[want]; balance -= want; } if(balance == 0) std::cout << ans << "\n"; else std::cout << "NIE\n"; return 0; } |