Created
July 4, 2016 02:17
-
-
Save jianminchen/d8879283c292454e2638444328633f09 to your computer and use it in GitHub Desktop.
AorB HackerRank C++ code study #2
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> | |
#define all(x) (x).begin(), (x).end() | |
#define li long long | |
#define itn int | |
using namespace std; | |
inline int nxt(){ | |
int n; | |
scanf("%d", &n); | |
return n; | |
} | |
int gcd(int x, int y){ | |
while (y){ | |
x %= y; | |
swap(x, y); | |
} | |
return x; | |
} | |
const int mod = 1000000007; | |
vector<bool> getlol(){ | |
string s; | |
cin >> s; | |
vector<bool> res; | |
for (auto c : s){ | |
int x = (c >= '0' && c <= '9') ? c - '0' : c - 'A' + 10; | |
for (int i = 0; i < 4; i++){ | |
res.push_back((x & (1 << (3 - i))) ? 1 : 0); | |
} | |
} | |
return res; | |
} | |
string xui(const vector<bool>& a){ | |
string res = ""; | |
for (int i = 0; i < a.size(); i += 4){ | |
int x = 0; | |
for (int j = 0; j < 4; j++){ | |
x *= 2; | |
if (a[i + j]) | |
x++; | |
} | |
res += (x < 10 ? '0' + x : 'A' + (x - 10)); | |
} | |
int i = 0; | |
while (i < (int)res.length() - 1 && res[i] == '0') | |
i++; | |
return res.substr(i); | |
} | |
int main(){ | |
cin.sync_with_stdio(0); | |
cout.sync_with_stdio(0); | |
int T; | |
cin >> T; | |
while (T--){ | |
int k; | |
cin >> k; | |
auto a = getlol(); | |
auto b = getlol(); | |
auto c = getlol(); | |
int n = a.size(); | |
if (b.size() > n) | |
n = b.size(); | |
if (c.size() > n) | |
n = c.size(); | |
reverse(all(a)); | |
reverse(all(b)); | |
reverse(all(c)); | |
a.resize(n, 0); | |
b.resize(n, 0); | |
c.resize(n, 0); | |
reverse(all(a)); | |
reverse(all(b)); | |
reverse(all(c)); | |
for (int i = 0; i < n; i++){ | |
if (c[i] == 0){ | |
if (a[i]){ | |
a[i] = 0; | |
k--; | |
} | |
if (b[i]){ | |
b[i] = 0; | |
k--; | |
} | |
} else { | |
if (a[i] == 0 && b[i] == 0){ | |
b[i] = 1; | |
k--; | |
} | |
} | |
} | |
if (k < 0){ | |
cout << "-1\n"; | |
} else { | |
for (int i = 0; i < n && k; i++){ | |
if (c[i]){ | |
if (a[i] && b[i]){ | |
a[i] = 0; | |
k--; | |
} else if (a[i]){ | |
if (k >= 2){ | |
a[i] = 0; | |
b[i] = 1; | |
k -= 2; | |
} | |
} | |
} | |
} | |
cout << xui(a) << "\n" << xui(b) << "\n"; | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment