Advertisement
Guest User

CH10 KeyCombos

a guest
Jun 20th, 2011
1,628
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.64 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.HashMap;
  5. import java.util.HashSet;
  6. import java.util.Map;
  7. import java.util.Set;
  8. import java.util.Map.Entry;
  9.  
  10. /*
  11.  * Tuenti Contest
  12.  * Challenge 10 - Key Combos
  13.  * Author: Pedro Antonio Pardal Jimena
  14.  * Email: pardal@alu.uma.es
  15.  */
  16.  
  17. public class KeyCombos
  18. {
  19.     private static Map<Set<String>, String> Combos;
  20.        
  21.     private static Set<String> getConjuntoTeclas( String combo )
  22.     {
  23.         Set<String> teclas = new HashSet<String>();
  24.        
  25.         for ( String tecla : combo.split( " " ) )
  26.         {
  27.             teclas.add( tecla );
  28.         }
  29.        
  30.         return teclas;
  31.     }
  32.    
  33.     public static void main( String[] args )
  34.     {
  35.         BufferedReader reader = new BufferedReader( new InputStreamReader( System.in ) );
  36.        
  37.         try
  38.         {
  39.             int ncombos = Integer.parseInt( reader.readLine().trim() );
  40.            
  41.             Combos = new HashMap<Set<String>, String>();
  42.            
  43.             for ( int i = 0; i < ncombos; i++ )
  44.             {
  45.                 String combo = reader.readLine().trim();
  46.                 String accion = reader.readLine().trim();
  47.  
  48.                 Combos.put( getConjuntoTeclas( combo ), accion );
  49.             }
  50.            
  51.             int ncasos = Integer.parseInt( reader.readLine().trim() );
  52.            
  53.             for ( int i = 0; i < ncasos; i++ )
  54.             {
  55.                 String combo = reader.readLine().trim();
  56.                
  57.                 Set<String> teclas = getConjuntoTeclas( combo );
  58.                
  59.                 for ( Entry<Set<String>, String> entry : Combos.entrySet() )
  60.                 {
  61.                     if ( entry.getKey().equals( teclas ) )
  62.                     {
  63.                         System.out.println( entry.getValue() );
  64.                         break;
  65.                     }
  66.                 }
  67.             }
  68.         }
  69.         catch ( IOException e )
  70.         {
  71.             System.err.println( "Formato de la entrada incorrecto" );
  72.         }
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement