Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created July 3, 2016 23:47
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/2f346cb9230967708b9db0154563cc61 to your computer and use it in GitHub Desktop.
Save jianminchen/2f346cb9230967708b9db0154563cc61 to your computer and use it in GitHub Desktop.
AorB HackerRank study code - C# No.4
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
class Solution {
static void Main(String[] args) {
map = new Dictionary<char, bool[]>();
map.Add('0', new bool[] { false, false, false, false });
map.Add('1', new bool[] { false, false, false, true });
map.Add('2', new bool[] { false, false, true, false });
map.Add('3', new bool[] { false, false, true, true });
map.Add('4', new bool[] { false, true, false, false });
map.Add('5', new bool[] { false, true, false, true });
map.Add('6', new bool[] { false, true, true, false });
map.Add('7', new bool[] { false, true, true, true });
map.Add('8', new bool[] { true, false, false, false });
map.Add('9', new bool[] { true, false, false, true });
map.Add('A', new bool[] { true, false, true, false });
map.Add('B', new bool[] { true, false, true, true });
map.Add('C', new bool[] { true, true, false, false });
map.Add('D', new bool[] { true, true, false, true });
map.Add('E', new bool[] { true, true, true, false });
map.Add('F', new bool[] { true, true, true, true });
pam = new Dictionary<string, char>();
pam.Add("0000", '0');
pam.Add("0001", '1');
pam.Add("0010", '2');
pam.Add("0011", '3');
pam.Add("0100", '4');
pam.Add("0101", '5');
pam.Add("0110", '6');
pam.Add("0111", '7');
pam.Add("1000", '8');
pam.Add("1001", '9');
pam.Add("1010", 'A');
pam.Add("1011", 'B');
pam.Add("1100", 'C');
pam.Add("1101", 'D');
pam.Add("1110", 'E');
pam.Add("1111", 'F');
int q = int.Parse(Console.ReadLine());
while (q-- > 0) {
int k = int.Parse(Console.ReadLine());
bool[] A = new bool[200005], B = new bool[200005], C = new bool[200005];
binary(Console.ReadLine(), A);
binary(Console.ReadLine(), B);
binary(Console.ReadLine(), C);
for (int i = 0; i < 200005; i++) {
bool c = C[i];
bool a = A[i];
bool b = B[i];
if (!c && !a && !b) continue;
if (c && (a || b)) continue;
if (!c) {
if (a) {
if (k > 0) { A[i] = !A[i]; k--; } else break;
}
if (b) {
if (k > 0) { B[i] = !B[i]; k--; } else break;
}
} else {
if (!a && !b) {
if (k > 0) {
B[i] = !B[i];
k--;
} else break;
}
}
}
if (k > 0) {
for (int i = 200004; i >= 0; i--) {
bool a = A[i];
bool b = B[i];
if (a && !b) {
if (k > 1) {
A[i] = !A[i];
B[i] = !B[i];
k -= 2;
}
} else if (a && b) {
if (k > 0) {
A[i] = !A[i];
k--;
} else break;
}
}
}
if (ABC(A, B, C)) {
StringBuilder sb = new StringBuilder();
bool xx = false;
for (int i = 200003; i >= 0; i -= 4) {
string g = A[i ] ? "1" : "0";
g += A[i - 1] ? "1" : "0";
g += A[i -2 ] ? "1" : "0";
g += A[i-3] ? "1" : "0";
xx = xx | (g != "0000");
if (xx) sb.Append(pam[g]);
}
Console.WriteLine(clean(sb.ToString()));
sb = new StringBuilder();
xx = false;
for (int i = 200003; i >= 0; i -= 4) {
string g = B[i ] ? "1" : "0";
g += B[i - 1] ? "1" : "0";
g += B[i - 2] ? "1" : "0";
g += B[i-3] ? "1" : "0";
xx = xx | (g != "0000");
if(xx) sb.Append(pam[g]);
}
Console.WriteLine(clean(sb.ToString()));
} else {
Console.WriteLine(-1);
}
}
}
static Dictionary<char, bool[]> map; static Dictionary<string, char> pam;
static string clean(string p) {
if (p == "") return "0";
if (p.Length < 2) return p;
int i = 0;
while (i < p.Length && p[i] == 48) i++;
if (i >= p.Length) return "0";
return p.Substring(i);
}
static bool ABC(bool[] A, bool[] B, bool[] C) {
for (int i = 0; i < 200005; i++) {
if ((A[i] | B[i]) != C[i]) return false;
}
return true;
}
static void binary(string x, bool[] w) {
int r = 0;
for (int i = x.Length - 1; i >= 0; i--) {
var g = map[x[i]];
w[r] = g[3];
w[r + 1] = g[2];
w[r + 2] = g[1];
w[r + 3] = g[0];
r += 4;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment