Created
July 4, 2016 02:00
-
-
Save jianminchen/4a608ffd202433f9be264c392f1bd75a to your computer and use it in GitHub Desktop.
AorB - HackerRank - C++ code study #1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
using namespace std; | |
#define rep(i, from, to) for (int i = from; i < (to); ++i) | |
#define trav(a, x) for (auto& a : x) | |
#define all(x) x.begin(), x.end() | |
#define sz(x) (int)(x).size() | |
typedef long long ll; | |
typedef pair<int, int> pii; | |
typedef vector<int> vi; | |
void PR(vi &v) { trav(x, v) cout << x << ' '; cout << endl; } | |
void unhex(char& c) { | |
if (0 <= c && c < 10) c = (char)('0' + c); | |
else c = (char)('A' + (c - 10)); | |
} | |
void rehex(char& c) { | |
if ('0' <= c && c <= '9') c = (char)(c - '0'); | |
else c = (char)(c - 'A' + 10); | |
} | |
string trim0(string& s) { | |
trav(x, s) unhex(x); | |
rep(i,0,sz(s)) { | |
if (s[i] != '0') return s.substr(i); | |
} | |
return "0"; | |
} | |
bool solve() { | |
int K, N; | |
string a, b, c; | |
cin >> K; | |
cin >> a >> b >> c; | |
N = max(max(sz(a), sz(b)), sz(c)); | |
a = string(N-sz(a), '0') + a; | |
b = string(N-sz(b), '0') + b; | |
c = string(N-sz(c), '0') + c; | |
trav(x, a) rehex(x); | |
trav(x, b) rehex(x); | |
trav(x, c) rehex(x); | |
int bits[4] = {8,4,2,1}; | |
rep(i,0,N) trav(bi, bits) { | |
if (!(c[i]&bi)) { | |
if (a[i]&bi) { | |
a[i] &= ~bi; | |
--K; | |
} | |
if (b[i]&bi) { | |
b[i] &= ~bi; | |
--K; | |
} | |
} | |
else { | |
if (!((a[i]&bi) || (b[i]&bi))) { | |
b[i] |= bi; | |
--K; | |
} | |
} | |
} | |
if (K < 0) return false; | |
rep(i,0,N) trav(bi, bits) { | |
if (c[i]&bi) { | |
if ((a[i]&bi) && (b[i]&bi) && K >= 1) { | |
a[i] &= ~bi; | |
K--; | |
} | |
else if ((a[i]&bi) && K >= 2) { | |
a[i] &= ~bi; | |
b[i] |= bi; | |
K -= 2; | |
} | |
} | |
} | |
cout << trim0(a) << endl; | |
cout << trim0(b) << endl; | |
return true; | |
} | |
int main() { | |
cin.sync_with_stdio(false); | |
cin.exceptions(cin.failbit); | |
int N; | |
cin >> N; | |
rep(i,0,N) { | |
if (!solve()) cout << -1 << endl; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment