Advertisement
javipinero

Key Combos Java Tuenti Contest

Jun 21st, 2011
1,576
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.65 KB | None | 0 0
  1. /************************************
  2.  ** Fichero KeyCombos.java
  3.  ***********************************/
  4.  
  5. package keyCombos;
  6.  
  7. import java.util.StringTokenizer;
  8.  
  9. public class KeyCombos {
  10.  
  11.     public static int maximoTeclas = 100;
  12.  
  13.     private String[] nombreTeclas;
  14.     private String[] nombreFunciones;
  15.     private boolean[][] combo;
  16.     private int numeroFunciones;
  17.     private int numeroFuncionesIntroducidas;
  18.  
  19.     public KeyCombos(int funciones) {
  20.  
  21.         nombreTeclas = new String[100];
  22.         numeroFunciones = funciones;
  23.         nombreFunciones = new String[numeroFunciones];
  24.         combo = new boolean[maximoTeclas][numeroFunciones];
  25.  
  26.         for (int i = 0; i < maximoTeclas; i++) {
  27.             for (int j = 0; j < numeroFunciones; j++) {
  28.                 combo[i][j] = false;
  29.             }
  30.         }
  31.  
  32.         numeroFuncionesIntroducidas = 0;
  33.  
  34.     }
  35.  
  36.     public void introducirCombo(String combinacion, String funcion) {
  37.  
  38.         nombreFunciones[numeroFuncionesIntroducidas] = funcion;
  39.  
  40.         StringTokenizer teclas = new StringTokenizer(combinacion);
  41.         while (teclas.hasMoreTokens()) {
  42.             String nombreTecla = teclas.nextToken();
  43.             int posicion;
  44.             if ((posicion = obtenerPosicionTecla(nombreTecla)) == -1) {
  45.                 posicion = introducirNombreTecla(nombreTecla);
  46.             }
  47.             combo[posicion][numeroFuncionesIntroducidas] = true;
  48.         }
  49.  
  50.         numeroFuncionesIntroducidas++;
  51.  
  52.     }
  53.  
  54.     public String obtenerCombo(String combinacion) {
  55.  
  56.         StringTokenizer teclas = new StringTokenizer(combinacion);
  57.  
  58.         int posicionesTecla[] = new int[teclas.countTokens()];
  59.        
  60.         int i = 0;
  61.         while (teclas.hasMoreTokens()) {
  62.             String nombreTecla = teclas.nextToken();
  63.             posicionesTecla[i] = obtenerPosicionTecla(nombreTecla);
  64.             i++;
  65.         }
  66.        
  67.         for (int j=0; j<numeroFunciones; j++) {
  68.             if (esFuncion(posicionesTecla, j)) {
  69.                 return nombreFunciones[j];
  70.             }
  71.         }
  72.        
  73.         return "";
  74.     }
  75.  
  76.     /*
  77.      * Introduce una nueva tecla en el String y devuelve la posicion en la que
  78.      * la ha introducido
  79.      */
  80.     private int introducirNombreTecla(String nombre) {
  81.  
  82.         int hashCode = nombre.hashCode() % 100;
  83.         while (nombreTeclas[hashCode] != null) {
  84.             hashCode = (hashCode + 1) % 100;
  85.         }
  86.         nombreTeclas[hashCode] = nombre;
  87.         return hashCode;
  88.  
  89.     }
  90.  
  91.     /*
  92.      * Obtiene la posicion de la tecla, si no existe devuelve -1.
  93.      */
  94.     private int obtenerPosicionTecla(String nombre) {
  95.  
  96.         int hashCode = nombre.hashCode() % 100;
  97.         while (nombreTeclas[hashCode] != null
  98.                 && !nombreTeclas[hashCode].equals(nombre)) {
  99.             hashCode = (hashCode + 1) % 100;
  100.         }
  101.         if (nombreTeclas[hashCode] == null) {
  102.             return -1;
  103.         }
  104.         return hashCode;
  105.     }
  106.    
  107.     private boolean esFuncion(int[] posicionesTecla, int numeroFuncion) {
  108.        
  109.         for (int i=0; i<posicionesTecla.length; i++) {
  110.             if (!combo[posicionesTecla[i]][numeroFuncion])
  111.                 return false;
  112.         }
  113.         return true;       
  114.     }
  115.  
  116. }
  117.  
  118. /************************************
  119.  ** Fichero Main.java
  120.  ***********************************/
  121.  
  122. package keyCombos;
  123.  
  124. import java.io.BufferedReader;
  125. import java.io.InputStreamReader;
  126.  
  127. public class Main {
  128.  
  129.     public static void main(String[] args) {
  130.        
  131.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  132.        
  133.         try {
  134.            
  135.             while (br.ready()) {
  136.                 int numCombos = Integer.parseInt(br.readLine());
  137.                 KeyCombos keyCombos = new KeyCombos(numCombos);
  138.                 for (int i=0; i<numCombos; i++) {
  139.                     String combo = br.readLine();
  140.                     String funcion = br.readLine().trim();
  141.                     keyCombos.introducirCombo(combo, funcion);
  142.                 }
  143.                 int numTest = Integer.parseInt(br.readLine().trim());
  144.                 for (int i=0; i<numTest; i++) {
  145.                     String combo = br.readLine();
  146.                     System.out.println(keyCombos.obtenerCombo(combo));
  147.                 }
  148.             }
  149.         } catch (Exception e) {
  150.             e.printStackTrace();
  151.         }
  152.  
  153.     }
  154.  
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement