Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created July 3, 2016 23:42
Show Gist options
  • Save jianminchen/4f7f354675d9d762d1493de0213a0342 to your computer and use it in GitHub Desktop.
Save jianminchen/4f7f354675d9d762d1493de0213a0342 to your computer and use it in GitHub Desktop.
AorB Csharp ocde - study code
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace AOrB
{
public static class Solution
{
public static string FromBinToHex(this string bininput)
{
StringBuilder output = new StringBuilder();
for (int i = 0; i < bininput.Length; i += 4)
{
output.Append(Convert.ToInt32(bininput.Substring(i, 4), 2).ToString("X"));
}
return output.ToString();
}
public class Query
{
public string A { get; set; }
public string B { get; set; }
public string C { get; set; }
public int K { get; set; }
private void Calculate()
{
#region Padding
int width = Math.Max(A.Length, Math.Max(B.Length, C.Length));
A = A.PadLeft(width, '0');
B = B.PadLeft(width, '0');
C = C.PadLeft(width, '0');
#endregion
StringBuilder a_builder = new StringBuilder();
StringBuilder b_builder = new StringBuilder();
for (int i = 0; i < C.Length; i++)
{
var aint = Convert.ToInt32(A[i].ToString(), 16);
var bint = Convert.ToInt32(B[i].ToString(), 16);
var cint = Convert.ToInt32(C[i].ToString(), 16);
if (aint == bint && bint == cint)
{
a_builder.Append(Convert.ToString(aint, 2).PadLeft(4, '0'));
b_builder.Append(Convert.ToString(aint, 2).PadLeft(4, '0'));
continue;
}
var a1 = Convert.ToString(aint, 2).PadLeft(4, '0').ToArray();
var b1 = Convert.ToString(bint, 2).PadLeft(4, '0').ToArray();
var c1 = Convert.ToString(cint, 2).PadLeft(4, '0').ToArray();
for (int ii = 0; ii < 4; ii++)
{
if (c1[ii] == '1')
{
if (a1[ii] == '0' && b1[ii] == '0')
{
b1[ii] = '1';
K--;
}
}
else
{
if (a1[ii] == '1')
{
a1[ii] = '0';
K--;
}
if (b1[ii] == '1')
{
b1[ii] = '0';
K--;
}
}
}
if (K < 0)
break;
a_builder.Append(string.Join("", a1));
b_builder.Append(string.Join("", b1));
}
if (K == 0)
{
Ab = a_builder.ToString().FromBinToHex().TrimStart('0');
Bb = b_builder.ToString().FromBinToHex().TrimStart('0');
}
else if (K > 0)
{
//Then we should be able to cancel out some one
var a_array = a_builder.ToString().ToArray();
var b_array = b_builder.ToString().ToArray();
for (var i = 0; i < a_array.Length; i++)
{
if (a_array[i] == '1')
{
if (b_array[i] == '1')
{
a_array[i] = '0';
K--;
}
else if (K >= 2)
{
a_array[i] = '0';
b_array[i] = '1';
K -= 2;
}
if (K == 0)
break;
}
}
Ab = string.Join("", a_array).FromBinToHex().TrimStart('0').PadLeft(1, '0');
Bb = string.Join("", b_array).FromBinToHex().TrimStart('0').PadLeft(1, '0');
}
}
private string Ab;
private string Bb;
public void WriteResult(IConsole console)
{
Calculate();
if (string.IsNullOrEmpty(Ab))
console.WriteLine("-1");
else
{
console.WriteLine(Ab.ToUpper());
console.WriteLine(Bb.ToUpper());
}
}
}
public static void Main(String[] args)
{
int queries = int.Parse(IOConsole.ReadLine());
List<Query> qlist = new List<Query>();
for (int i = 0; i < queries; i++)
{
qlist.Add(new Query
{
K = int.Parse(IOConsole.ReadLine()),
A = IOConsole.ReadLine(),
B = IOConsole.ReadLine(),
C = IOConsole.ReadLine()
});
}
foreach (var q in qlist)
q.WriteResult(IOConsole);
}
public static IConsole IOConsole = new MyConsole();
}
public interface IConsole
{
string ReadLine();
void WriteLine(string format, params object[] args);
}
public class MyConsole : IConsole
{
public string ReadLine()
{
return Console.ReadLine();
}
public void WriteLine(string format, params object[] args)
{
Console.WriteLine(format, args);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment