UVA 11340 – Newspaper

Super easy problem if you’re used to a bit of C++ and familiar to the printf method.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.StringTokenizer;
 
/**
 *
 * @author Sanchit M. Bhatnagar
 * @see http://uhunt.felix-halim.net/id/74004
 *
 */
public class P11340 {
 
  public static void main(String[] args) throws NumberFormatException, IOException {
    BufferedReader sc = new BufferedReader(new InputStreamReader(System.in));
    PrintWriter out = new PrintWriter(System.out);
 
    int N = Integer.parseInt(sc.readLine());
    for (int zz = 0; zz < N; zz++) {
      int K = Integer.parseInt(sc.readLine());
      HashMap<Character, Integer> map = new HashMap<Character, Integer>();
      StringTokenizer st = null;
      for (int yy = 0; yy < K; yy++) {
        st = new StringTokenizer(sc.readLine());
        map.put(st.nextToken().charAt(0), Integer.parseInt(st.nextToken()));
      }
      double ans = 0;
      int M = Integer.parseInt(sc.readLine());
      for (int xx = 0; xx < M; xx++) {
        char[] line = sc.readLine().toCharArray();
        for (Character c : line) {
          if (map.containsKey(c)) {
            ans += map.get(c);
          }
        }
      }
      ans /= 100;
      out.printf("%.2f$" + System.lineSeparator(), ans);
    }
 
    out.close();
    sc.close();
  }
}

4 thoughts on “UVA 11340 – Newspaper”

    1. Thanks for your comment. So firstly you should change the name of the class to “Main” before submitting and secondly try copy pasting into notepad before submitting it. Apparently there are some invisible characters (character code 160) which are showing up in the code. I’ll just fix it. (or well hopefully fix it). Can’t really see invisible characters :)

  1. Thank you for your reply at u-hunt ;) My solutions are now getting accepted by the judge. Also, for this code, I have tried the Scanner class instead of BufferedReader with the same idea as yours but my answer is not correct. Could you help me know the fault in this code:

    import java.io.*;
    import java.util.Hashtable;
    import java.util.Scanner;
    class Newspaper {
    public static void main(String[] args) throws IOException {
    Scanner sc=new Scanner(System.in);
    int t=sc.nextInt(); // no of test cases
    int i;
    for(i=0;i<t;i++){
    Hashtable h=new Hashtable();
    int x=sc.nextInt(); //no of hashtable entries
    Character a = null;
    for(int m=0;m<x;m++){
    a=sc.next().charAt(0); //taking hashtable entries
    Integer y=sc.nextInt();
    h.put(a,y); // hashtable created
    }
    String s;
    Double sum=0.0;
    int b=sc.nextInt(); //number of lines in the text
    for(int j=0;j<b;j++){
    s=sc.nextLine(); //reading one line of the text
    char[] v=s.toCharArray();
    for(char c:v){
    if(h.containsKey(c)){
    sum =sum + (h.get(c));
    }
    }
    }
    System.out.printf("%.2f$",sum/100);
    System.out.println();

    }
    }
    }

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.