Advertisement
abouttr3

Asia Regional KL 2014 (J)

Nov 1st, 2014
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.42 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. //Team 19 - Newbiez Crew 3.0
  4.  
  5. public class J {
  6.     public static void main(String[] args) {
  7.         Scanner in = new Scanner(System.in);
  8.         StringBuffer sb = new StringBuffer();
  9.  
  10.         String hex = "00000000";
  11.         int n = Integer.parseInt(in.nextLine());
  12.         String result = "";
  13.         for (int i = 0; i < n; i++) {
  14.             // [\\s]+ this a regular expression(RegEx),
  15.             // \\s use to inform that the delimiter format is any blank space,
  16.             // + one or many...
  17.             String[] hexString = in.nextLine().split("[\\s]+");
  18.             String[] binaryString = new String[2];
  19.             for (int j = 0; j < binaryString.length; j++) {
  20.                 // Below line use to make the string become in 8 characters
  21.                 // format
  22.                 // by adding 0 at the front of input string
  23.                 // (i.e. str = "AAA", hex.substring(3), 3 is start index -> "00000",
  24.                 // finally "00000" + "AAA")
  25.                 hexString[j] = hex.substring(hexString[j].length()) + hexString[j];
  26.                 binaryString[j] = "";
  27.                 for (int k = 0; k < hexString[j].length(); k++) {
  28.                     binaryString[j] = binaryString[j].concat(binary(hex(hexString[j].charAt(k))));
  29.                 }
  30.  
  31.             }
  32.  
  33.             int right = match(0, binaryString[0], binaryString[1]);
  34.             int left = match(0, binaryString[1], binaryString[0]);
  35.  
  36.             if (right == -1)
  37.                 result = "Not possible";
  38.             else if (right == left)
  39.                 result = String.format("%d %s", right, "Any");
  40.             else if (right < left)
  41.                 result = String.format("%d %s", right, "Right");
  42.             else
  43.                 result = String.format("%d %s", left, "Left");
  44.            
  45.             sb.append(String.format("Case #%d: %s\n",i+1, result));
  46.  
  47.         }
  48.         System.out.print(sb.toString());
  49.     }
  50.  
  51.     private static int match(int start, String str1, String str2) {
  52.  
  53.         if (start < str1.length()) {
  54.             boolean isSame = true;
  55.             for (int i = start; i < start + str1.length(); i++) {
  56.                 // if char in str1 at position i - start is not equal char in
  57.                 // str2 at postion i % str1 length
  58.                 // i need modulus because the limit of loop is start + str1
  59.                 // length, means that there will have an extra loop with purpose
  60.                 // to make the string like a circle. So, it will compare every
  61.                 // character, even the start position is length - 1
  62.                 if (str1.charAt(i - start) != str2.charAt(i % str1.length())) {
  63.                     isSame = false;
  64.                     break;
  65.                 }
  66.             }
  67.             // if both string still can't match, the program will keep recursive
  68.             // until it found that both string is same, if the str2 start at X
  69.             // position,
  70.             // or the start is greater or equal to str1 length
  71.             if (!isSame)
  72.                 return match(start + 1, str1, str2);
  73.             else
  74.                 return start;
  75.         }
  76.         return -1;
  77.  
  78.     }
  79.  
  80.     private static String binary(int dec) {
  81.         String binary = "";
  82.         int bin = 0;
  83.  
  84.         // 16 hex <=> 1111, then the largest power in binary is n^3 and the
  85.         // lowest power in binary is n^0
  86.         // because of that the value of i is 3 -> 0
  87.         for (int i = 3; i >= 0; i--) {
  88.             bin = (int) Math.pow(2, i);
  89.             if (bin <= dec) {
  90.                 dec -= bin;
  91.                 binary += '1';
  92.             } else
  93.                 binary += '0';
  94.         }
  95.         return binary;
  96.     }
  97.  
  98.     private static int hex(char ch) {
  99.  
  100.         // if ch is '0' -> '9' , in ASCII Code, '0' is 48(Decimal) not 0
  101.         // to get 0 if ch is '0', then need minus ch with '0'
  102.         int num = ch - '0';
  103.  
  104.         // if ch is 'A' -> 'Z' , in ASCII Code, 'A' is 65(Decimal) and in hex,
  105.         // 'A' is 10
  106.         // to get 10 if ch is 'A', then need minus ch with 'A' and get 0, then
  107.         // plus by 10
  108.         if (ch >= 'A' && ch <= 'Z') {
  109.             num = ch - 'A' + 10;
  110.         }
  111.         return num;
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement