Monday, 28 July 2014

UVa 11340 - Newspaper

/*
 * @author Divyanshu
 * @mail divyanshu17x@gmail.com
 * @place GNDU RC SATHIALA
 * @date 28 / 07 / 2014
 * @judge uva.onlinejudge.org
 * @verdict Accepted
 * @problem 11340 - Newspaper
 * @problemType  Direct Mapping
 */

import java.io.*; 
import java.util.*; 
 class  Main {
     public static void main(String args[]) throws IOException{
            Reader.init(System.in);
            int testCases = Reader.nextInt() ;
            while(testCases-- > 0 )
            {
                // total hashtable entries
                int paidChars = Reader.nextInt();
               
                // total cents
                double ttlCents = 0;
               
                HashMap<Character , Integer> map = new HashMap<>();
               
                //loop through and fill the hashmap
                while(paidChars-- > 0 )
                {
                    // read the name value pair line and tokenize it
                    StringTokenizer st =  new StringTokenizer(Reader.reader.readLine());
                   
                    // fill the hashmap with  name  value tokens
                    map.put(st.nextToken().charAt(0), Integer.parseInt(st.nextToken()));
                   
                }
               
                //number of lines of text
                long ttlLines =  Reader.nextInt();
               
                //loop through and evaluate the characters in text for total money
                //raised by the autor of text
                while(ttlLines-- > 0 )
                {
                    String line = Reader.reader.readLine();
                    char arrayOfChars [] =  line.toCharArray();
                   
                    //for each character as key , validate the hashmap and update total dollars earned
                    for(char c : arrayOfChars)
                    {
                        if(map.containsKey(c))
                        {
                            ttlCents += map.get(c);
                        }
                    }
                }
               
           
           
                //show the dollars earned
                System.out.printf("%.2f$\n" , ttlCents/100);
            }
           
       }
   
 }



     /** Class for buffered reading int and double values */ 
     /** http://www.cpe.ku.ac.th/~jim/java-io.html */ 
     class Reader { 
          static BufferedReader reader; 
          static StringTokenizer tokenizer; 
          /** call this method to initialize reader for InputStream */ 
          static void init(InputStream input) { 
               reader = new BufferedReader(new InputStreamReader(input)); 
               tokenizer = new StringTokenizer(""); 
          } 
          /** get next word */ 
          static String next() throws IOException { 
               while (!tokenizer.hasMoreTokens()) { 
                    tokenizer = new StringTokenizer(reader.readLine()); 
               } 
               return tokenizer.nextToken(); 
          } 
          static int nextInt() throws IOException { 
               return Integer.parseInt(next()); 
          } 
          static double nextDouble() throws IOException { 
               return Double.parseDouble(next()); 
          } 
     }

No comments:

Post a Comment