Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created July 4, 2016 02:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/d65f78caec4c8953949639b70dbdf192 to your computer and use it in GitHub Desktop.
Save jianminchen/d65f78caec4c8953949639b70dbdf192 to your computer and use it in GitHub Desktop.
AorB study C++ - study code #3
#include<bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i = (a); i <= (b); ++i)
#define FORD(i,a,b) for(int i = (a); i >= (b); --i)
#define RI(i,n) FOR(i,1,(n))
#define REP(i,n) FOR(i,0,(n)-1)
#define mini(a,b) a=min(a,b)
#define maxi(a,b) a=max(a,b)
#define mp make_pair
#define pb push_back
#define st first
#define nd second
#define sz(w) (int) w.size()
typedef vector<int> vi;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
const int inf = 1e9 + 5;
const int nax = 1e7 + 5;
string solve(bool * t, int n) {
string s;
while(n > 0 && !t[n-1]) --n;
if(n == 0) return string("0");
while(n % 4) ++n;
for(int i = n - 4; i >= 0; i -= 4) {
int dig = 0;
REP(j, 4) if(t[i+j]) dig |= (1 << j);
if(dig <= 9) s += ('0' + dig);
else s += ('A' + (dig - 10));
}
return s;
}
char sl[nax];
bool t[3][nax];
int n[3];
int to_int(char znak) {
if('0' <= znak && znak <= '9')
return znak - '0';
else
return 10 + (znak - 'A');
}
void te() {
int k;
scanf("%d", &k);
REP(which, 3) {
scanf("%s", sl);
int len = strlen(sl);
n[which] = 0;
FORD(i, len - 1, 0) {
int dig = to_int(sl[i]);
REP(j, 4) t[which][n[which]++] = dig & (1 << j);
}
//REP(i, n[which]) printf("%d", (int) t[which][i]); puts("");
}
int max_n = max(n[0], max(n[1], n[2]));
REP(i, max_n) {
if(t[2][i]) {
if(!t[0][i] && !t[1][i]) {
--k;
t[1][i] = true;
}
}
else {
if(t[0][i]) {
t[0][i] = false;
--k;
}
if(t[1][i]) {
t[1][i] = false;
--k;
}
}
}
FORD(i, max_n - 1, 0) if(t[2][i] && k > 0) {
if(t[0][i]) {
if(t[1][i]) {
--k;
t[0][i] = false;
}
else if(k >= 2) {
k -= 2;
t[0][i] = false;
t[1][i] = true;
}
}
}
if(k < 0) {
puts("-1");
}
else {
printf("%s\n", solve(t[0], max_n).c_str());
printf("%s\n", solve(t[1], max_n).c_str());
}
REP(j,3) REP(i, max_n + 2) t[j][i] = false;
}
int main() {
int z;
scanf("%d", &z);
while(z--) te();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment