Guest User

CH10 KeyCombos

a guest
Jun 20th, 2011
1,718
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.  */
  15.  
  16. public class KeyCombos
  17. {
  18.     private static Map<Set<String>, String> Combos;
  19.        
  20.     private static Set<String> getConjuntoTeclas( String combo )
  21.     {
  22.         Set<String> teclas = new HashSet<String>();
  23.        
  24.         for ( String tecla : combo.split( " " ) )
  25.         {
  26.             teclas.add( tecla );
  27.         }
  28.        
  29.         return teclas;
  30.     }
  31.    
  32.     public static void main( String[] args )
  33.     {
  34.         BufferedReader reader = new BufferedReader( new InputStreamReader( System.in ) );
  35.        
  36.         try
  37.         {
  38.             int ncombos = Integer.parseInt( reader.readLine().trim() );
  39.            
  40.             Combos = new HashMap<Set<String>, String>();
  41.            
  42.             for ( int i = 0; i < ncombos; i++ )
  43.             {
  44.                 String combo = reader.readLine().trim();
  45.                 String accion = reader.readLine().trim();
  46.  
  47.                 Combos.put( getConjuntoTeclas( combo ), accion );
  48.             }
  49.            
  50.             int ncasos = Integer.parseInt( reader.readLine().trim() );
  51.            
  52.             for ( int i = 0; i < ncasos; i++ )
  53.             {
  54.                 String combo = reader.readLine().trim();
  55.                
  56.                 Set<String> teclas = getConjuntoTeclas( combo );
  57.                
  58.                 for ( Entry<Set<String>, String> entry : Combos.entrySet() )
  59.                 {
  60.                     if ( entry.getKey().equals( teclas ) )
  61.                     {
  62.                         System.out.println( entry.getValue() );
  63.                         break;
  64.                     }
  65.                 }
  66.             }
  67.         }
  68.         catch ( IOException e )
  69.         {
  70.             System.err.println( "Formato de la entrada incorrecto" );
  71.         }
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment