Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /************************************
- ** Fichero KeyCombos.java
- ***********************************/
- package keyCombos;
- import java.util.StringTokenizer;
- public class KeyCombos {
- public static int maximoTeclas = 100;
- private String[] nombreTeclas;
- private String[] nombreFunciones;
- private boolean[][] combo;
- private int numeroFunciones;
- private int numeroFuncionesIntroducidas;
- public KeyCombos(int funciones) {
- nombreTeclas = new String[100];
- numeroFunciones = funciones;
- nombreFunciones = new String[numeroFunciones];
- combo = new boolean[maximoTeclas][numeroFunciones];
- for (int i = 0; i < maximoTeclas; i++) {
- for (int j = 0; j < numeroFunciones; j++) {
- combo[i][j] = false;
- }
- }
- numeroFuncionesIntroducidas = 0;
- }
- public void introducirCombo(String combinacion, String funcion) {
- nombreFunciones[numeroFuncionesIntroducidas] = funcion;
- StringTokenizer teclas = new StringTokenizer(combinacion);
- while (teclas.hasMoreTokens()) {
- String nombreTecla = teclas.nextToken();
- int posicion;
- if ((posicion = obtenerPosicionTecla(nombreTecla)) == -1) {
- posicion = introducirNombreTecla(nombreTecla);
- }
- combo[posicion][numeroFuncionesIntroducidas] = true;
- }
- numeroFuncionesIntroducidas++;
- }
- public String obtenerCombo(String combinacion) {
- StringTokenizer teclas = new StringTokenizer(combinacion);
- int posicionesTecla[] = new int[teclas.countTokens()];
- int i = 0;
- while (teclas.hasMoreTokens()) {
- String nombreTecla = teclas.nextToken();
- posicionesTecla[i] = obtenerPosicionTecla(nombreTecla);
- i++;
- }
- for (int j=0; j<numeroFunciones; j++) {
- if (esFuncion(posicionesTecla, j)) {
- return nombreFunciones[j];
- }
- }
- return "";
- }
- /*
- * Introduce una nueva tecla en el String y devuelve la posicion en la que
- * la ha introducido
- */
- private int introducirNombreTecla(String nombre) {
- int hashCode = nombre.hashCode() % 100;
- while (nombreTeclas[hashCode] != null) {
- hashCode = (hashCode + 1) % 100;
- }
- nombreTeclas[hashCode] = nombre;
- return hashCode;
- }
- /*
- * Obtiene la posicion de la tecla, si no existe devuelve -1.
- */
- private int obtenerPosicionTecla(String nombre) {
- int hashCode = nombre.hashCode() % 100;
- while (nombreTeclas[hashCode] != null
- && !nombreTeclas[hashCode].equals(nombre)) {
- hashCode = (hashCode + 1) % 100;
- }
- if (nombreTeclas[hashCode] == null) {
- return -1;
- }
- return hashCode;
- }
- private boolean esFuncion(int[] posicionesTecla, int numeroFuncion) {
- for (int i=0; i<posicionesTecla.length; i++) {
- if (!combo[posicionesTecla[i]][numeroFuncion])
- return false;
- }
- return true;
- }
- }
- /************************************
- ** Fichero Main.java
- ***********************************/
- package keyCombos;
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- public class Main {
- public static void main(String[] args) {
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- try {
- while (br.ready()) {
- int numCombos = Integer.parseInt(br.readLine());
- KeyCombos keyCombos = new KeyCombos(numCombos);
- for (int i=0; i<numCombos; i++) {
- String combo = br.readLine();
- String funcion = br.readLine().trim();
- keyCombos.introducirCombo(combo, funcion);
- }
- int numTest = Integer.parseInt(br.readLine().trim());
- for (int i=0; i<numTest; i++) {
- String combo = br.readLine();
- System.out.println(keyCombos.obtenerCombo(combo));
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement