Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- //Team 19 - Newbiez Crew 3.0
- public class J {
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- StringBuffer sb = new StringBuffer();
- String hex = "00000000";
- int n = Integer.parseInt(in.nextLine());
- String result = "";
- for (int i = 0; i < n; i++) {
- // [\\s]+ this a regular expression(RegEx),
- // \\s use to inform that the delimiter format is any blank space,
- // + one or many...
- String[] hexString = in.nextLine().split("[\\s]+");
- String[] binaryString = new String[2];
- for (int j = 0; j < binaryString.length; j++) {
- // Below line use to make the string become in 8 characters
- // format
- // by adding 0 at the front of input string
- // (i.e. str = "AAA", hex.substring(3), 3 is start index -> "00000",
- // finally "00000" + "AAA")
- hexString[j] = hex.substring(hexString[j].length()) + hexString[j];
- binaryString[j] = "";
- for (int k = 0; k < hexString[j].length(); k++) {
- binaryString[j] = binaryString[j].concat(binary(hex(hexString[j].charAt(k))));
- }
- }
- int right = match(0, binaryString[0], binaryString[1]);
- int left = match(0, binaryString[1], binaryString[0]);
- if (right == -1)
- result = "Not possible";
- else if (right == left)
- result = String.format("%d %s", right, "Any");
- else if (right < left)
- result = String.format("%d %s", right, "Right");
- else
- result = String.format("%d %s", left, "Left");
- sb.append(String.format("Case #%d: %s\n",i+1, result));
- }
- System.out.print(sb.toString());
- }
- private static int match(int start, String str1, String str2) {
- if (start < str1.length()) {
- boolean isSame = true;
- for (int i = start; i < start + str1.length(); i++) {
- // if char in str1 at position i - start is not equal char in
- // str2 at postion i % str1 length
- // i need modulus because the limit of loop is start + str1
- // length, means that there will have an extra loop with purpose
- // to make the string like a circle. So, it will compare every
- // character, even the start position is length - 1
- if (str1.charAt(i - start) != str2.charAt(i % str1.length())) {
- isSame = false;
- break;
- }
- }
- // if both string still can't match, the program will keep recursive
- // until it found that both string is same, if the str2 start at X
- // position,
- // or the start is greater or equal to str1 length
- if (!isSame)
- return match(start + 1, str1, str2);
- else
- return start;
- }
- return -1;
- }
- private static String binary(int dec) {
- String binary = "";
- int bin = 0;
- // 16 hex <=> 1111, then the largest power in binary is n^3 and the
- // lowest power in binary is n^0
- // because of that the value of i is 3 -> 0
- for (int i = 3; i >= 0; i--) {
- bin = (int) Math.pow(2, i);
- if (bin <= dec) {
- dec -= bin;
- binary += '1';
- } else
- binary += '0';
- }
- return binary;
- }
- private static int hex(char ch) {
- // if ch is '0' -> '9' , in ASCII Code, '0' is 48(Decimal) not 0
- // to get 0 if ch is '0', then need minus ch with '0'
- int num = ch - '0';
- // if ch is 'A' -> 'Z' , in ASCII Code, 'A' is 65(Decimal) and in hex,
- // 'A' is 10
- // to get 10 if ch is 'A', then need minus ch with 'A' and get 0, then
- // plus by 10
- if (ch >= 'A' && ch <= 'Z') {
- num = ch - 'A' + 10;
- }
- return num;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement