Pages

1/19/2014

UVA 10374 Election solution

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.HashMap;


class Main {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);


int tc = sc.nextInt();

boolean ind = false;
for (int i =0 ;i < tc ;i++) {

sc.nextLine();
if(ind) System.out.println();
ind = true;
{
HashMap<String,String> cand = new HashMap<> ();
HashMap<String,Integer> vt_ctr= new HashMap<> ();
int p = sc.nextInt();
 sc.nextLine();

 for (int i1 =0; i1<p; i1++) {
 String candt = sc.nextLine();
 String part = sc.nextLine();
 if (part.equalsIgnoreCase("independent")) part = part+candt;
 cand.put(candt, part);
 }

 int votes = sc.nextInt();
 sc.nextLine();
 for(int i1 =0;i1<votes;i1++) {
 String cand1 = sc.nextLine();

 if (!vt_ctr.containsKey(cand1)) vt_ctr.put(cand1, (Integer)1);
 else {
 int tmp =1 + vt_ctr.get(cand1);
 vt_ctr.put(cand1,tmp);
 }
 }
 boolean flag = false;
 String winner=null;
 int max=0;
 Iterator<Entry<String, Integer>> it  = vt_ctr.entrySet().iterator();
while(it.hasNext()) {
Entry<String, Integer> entry = it.next();
 String key = entry.getKey();
 Integer value = entry.getValue();
 if(max<value) {
 max = value;
 winner = key;
 flag = false;
 }
 else if (max==value) {
 flag = true;
 }
}
if (flag) System.out.println("tie");
else
System.out.println(cand.get(winner));

}
}
}

}

No comments :

Post a Comment