Created
April 12, 2018 02:59
-
-
Save dbtorrico/5491e79db6da5dbc6823c454db6dc5d8 to your computer and use it in GitHub Desktop.
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
import java.io.*; | |
import java.math.*; | |
import java.text.*; | |
import java.util.*; | |
import java.util.regex.*; | |
public class Solution { | |
/* | |
* Complete the aOrB function below. | |
*/ | |
static void aOrB(int k, String a, String b, String c) { | |
List<String> a_array = new ArrayList<String>(Arrays.asList(transformaArray(a))); | |
List<String> b_array = new ArrayList<String>(Arrays.asList(transformaArray(b))); | |
List<String> c_array = new ArrayList<String>(Arrays.asList(transformaArray(c))); | |
int tamanhoMaiorArray = tamanhoMaiorArray(a_array, b_array, c_array); | |
//COMPLETA ARRAYS COM 0 | |
int size_a = a_array.size(); | |
int size_b = b_array.size(); | |
int size_c = c_array.size(); | |
for (int i = size_a; i < tamanhoMaiorArray; i++) { | |
a_array.add("0"); | |
} | |
for (int i = size_b; i < tamanhoMaiorArray; i++) { | |
b_array.add("0"); | |
} | |
for (int i = size_c; i < tamanhoMaiorArray; i++) { | |
c_array.add("0"); | |
} | |
if (checaArrays(tamanhoMaiorArray, a_array, b_array, c_array, k)) { | |
Collections.reverse(a_array); | |
Collections.reverse(b_array); | |
Collections.reverse(c_array); | |
//TRANSFORMA ARRAYS EM STRINGS | |
StringBuilder sb = new StringBuilder(); | |
for (String s : a_array) { | |
sb.append(s); | |
} | |
String str_a_linha = sb.toString(); | |
StringBuilder sb1 = new StringBuilder(); | |
for (String s : b_array) { | |
sb1.append(s); | |
} | |
String str_b_linha = sb1.toString(); | |
StringBuilder sb2 = new StringBuilder(); | |
for (String s : c_array) { | |
sb2.append(s); | |
} | |
String str_c_linha = sb2.toString(); | |
// System.out.println("A' Binario:" + str_a_linha); | |
// System.out.println("B' Binario:" + str_b_linha); | |
// System.out.println("C. Binario:" + str_c_linha); | |
BigInteger a_linha_bin = new BigInteger(str_a_linha, 2); | |
BigInteger b_linha_bin = new BigInteger(str_b_linha, 2); | |
System.out.println(a_linha_bin.toString(16).toUpperCase()); | |
System.out.println(b_linha_bin.toString(16).toUpperCase()); | |
} else { | |
System.out.println("-1"); | |
} | |
} | |
static boolean checaArrays(int tamanhoMaiorArray, List<String> a_array, List<String> b_array, List<String> c_array, int k) { | |
int bit_a = 0; | |
int bit_b = 0; | |
int bit_c = 0; | |
int changed = 0; | |
for (int i = 0; i < tamanhoMaiorArray; i++) { | |
if (changed <= k) { | |
bit_a = Integer.parseInt(a_array.get(i)); | |
bit_b = Integer.parseInt(b_array.get(i)); | |
bit_c = Integer.parseInt(c_array.get(i)); | |
if ((bit_a | bit_b) != bit_c) { | |
if ((bit_a + bit_b == 2) && (k - changed >= 2)) { | |
a_array.set(i, "0"); | |
b_array.set(i, "0"); | |
changed += 2; | |
} else if (bit_a == 0 && bit_b == 1) { | |
b_array.set(i, "0"); | |
changed++; | |
} else if (bit_a == 1 && bit_b == 0) { | |
a_array.set(i, "0"); | |
changed++; | |
} else if (bit_a + bit_b == 0) { | |
b_array.set(i, "1"); | |
changed++; | |
//a_array.set(i, "1"); | |
} | |
} | |
} else { | |
return false; | |
} | |
} | |
//OTIMIZANDO | |
for (int i = tamanhoMaiorArray - 1; i >= 0; i--) { | |
if (changed < k) { | |
bit_a = Integer.parseInt(a_array.get(i)); | |
bit_b = Integer.parseInt(b_array.get(i)); | |
bit_c = Integer.parseInt(c_array.get(i)); | |
if ((bit_a == 1 && bit_b == 0) && (k - changed >= 2)) { | |
a_array.set(i, "0"); | |
b_array.set(i, "1"); | |
changed += 2; | |
} else if (bit_a + bit_b == 2) { | |
a_array.set(i, "0"); | |
changed++; | |
} | |
} | |
} | |
return true; | |
} | |
static String[] transformaArray(String s) { | |
BigInteger temp = new BigInteger(s, 16); | |
String bin = temp.toString(2); | |
String[] bin_array = bin.split(""); | |
reverseArray(bin_array); | |
return bin_array; | |
} | |
static int tamanhoMaiorArray(List<String> a, List<String> b, List<String> c) { | |
if (a.size() > b.size()) { | |
if (a.size() > c.size()) { | |
return a.size(); | |
} else { | |
return c.size(); | |
} | |
} else if (b.size() > c.size()) { | |
return b.size(); | |
} else { | |
return c.size(); | |
} | |
} | |
static void reverseArray(String[] a) { | |
for (int i = 0; i < a.length / 2; i++) { | |
String temp = a[i]; | |
a[i] = a[a.length - i - 1]; | |
a[a.length - i - 1] = temp; | |
} | |
} | |
@SuppressWarnings("empty-statement") | |
public static void main(String[] args) throws IOException { | |
int bufferSize = 512 * 1024; | |
BufferedReader rd = new BufferedReader(new InputStreamReader(System.in), bufferSize); | |
String a; | |
String b; | |
String c; | |
while (!rd.ready()); | |
long startTime = System.nanoTime(); | |
int q = Integer.parseInt(rd.readLine()); | |
for (int qItr = 0; qItr < q; qItr++) { | |
try { | |
int k = Integer.parseInt(rd.readLine()); | |
a = rd.readLine(); | |
b = rd.readLine(); | |
c = rd.readLine(); | |
aOrB(k, a, b, c); | |
} catch (IOException ex) { | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment