Advertisement
alefhidalgo

KeyCombos

Jun 20th, 2011
1,503
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.86 KB | None | 0 0
  1. import java.security.MessageDigest;
  2. import java.security.NoSuchAlgorithmException;
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5. import java.util.HashMap;
  6. import java.util.List;
  7. import java.util.Map;
  8. import java.util.Scanner;
  9.  
  10. /**
  11.  * Tuenti Programming Contest
  12.  * Challenge 10: Key Combos
  13.  * @author alefhidalgo [at] gmail [dot] com
  14.  */
  15. public class KeyCombos {
  16.    
  17.     private static final String HEXES = "0123456789ABCDEF";
  18.     private static Map<String,String> combinationMap;
  19.     static {
  20.         combinationMap = new HashMap<String,String>();
  21.     }
  22.    
  23.     /**
  24.      * Unique Hash Code for each combinationKeys
  25.      * @param combinationKeys
  26.      * @return
  27.      */
  28.     private static String getHashCode(List<String> combinationKeys) {
  29.         Collections.sort(combinationKeys); //all orders are indexes as same hash
  30.         try{
  31.         MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
  32.         String toEncode = "";
  33.         for (String string : combinationKeys) {
  34.             toEncode+=string;
  35.         }
  36.         digest.update(toEncode.getBytes());
  37.         byte[] hash = digest.digest();
  38.         return getHex(hash);
  39.         } catch ( NoSuchAlgorithmException e) {
  40.             /* Never should happen */
  41.             return null;
  42.         }
  43.     }
  44.     public static String getHex(byte[] raw) {
  45.         if (raw == null) {
  46.             return null;
  47.         }
  48.         final StringBuilder hex = new StringBuilder(2 * raw.length);
  49.         for (final byte b : raw) {
  50.             hex.append(HEXES.charAt((b & 0xF0) >> 4)).append(
  51.                     HEXES.charAt((b & 0x0F)));
  52.         }
  53.         return hex.toString();
  54.     }
  55.     /**
  56.      * getCommand from combinationKeys
  57.      * @param combinationKeys
  58.      * @return
  59.      */
  60.     private static String getCommand(List<String> combinationKeys){
  61.         return combinationMap.get(getHashCode(combinationKeys));       
  62.     }
  63.    
  64.     /**
  65.      * addShortCut into combinationMap
  66.      * @param commandName
  67.      * @param combinationKeys
  68.      */
  69.     private static void addShortCut(String commandName, List<String> combinationKeys){     
  70.         combinationMap.put(getHashCode(combinationKeys), commandName);
  71.     }
  72.    
  73.     public static void main(String args[]) {       
  74.         Scanner in = new Scanner(System.in);
  75.         Scanner lineScanner = null;
  76.         List<String> combinationKeys = null;
  77.         String commandName = null;
  78.         int nKeyCombos = Integer.parseInt(in.nextLine().trim());   
  79.         for(int i=0;i<nKeyCombos;i++){         
  80.             lineScanner = new Scanner(in.nextLine());
  81.             combinationKeys = new ArrayList<String>();
  82.             while(lineScanner.hasNext()){
  83.                 combinationKeys.add(lineScanner.next());
  84.             }
  85.             commandName = in.nextLine().trim();
  86.             //Map shortcut
  87.             addShortCut(commandName,combinationKeys);
  88.         }
  89.         int nTestCases = Integer.parseInt(in.nextLine().trim());
  90.         for (int i = 0; i < nTestCases; i++) {
  91.             lineScanner = new Scanner(in.nextLine());
  92.             combinationKeys = new ArrayList<String>();
  93.             while(lineScanner.hasNext()){
  94.                 combinationKeys.add(lineScanner.next());
  95.             }
  96.             //return command
  97.             System.out.println(getCommand(combinationKeys));
  98.         }
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement