Pages

Tuesday 1 January 2013

UVA - 11239 - Open Source

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuffer sb = new StringBuffer("");
        boolean flag=true;
        while(true){
            HashMap <String,Integer> hm=new  HashMap<String, Integer>();
            HashMap <String,String> id=new HashMap <String,String>();
            LinkedList<String> prj=new  LinkedList<String>();
            String last="";
            while (true) {
                    String temp=br.readLine().trim();
                    if(temp.equals("1")){
                        break;
                    }
                    if(temp.equals("0")){
                        flag=false;
                        break;
                    }
                    if(temp.charAt(0)>='a' &&temp.charAt(0)<='z'){
                        if(!id.containsKey(temp)){
                            id.put(temp, last);
                            int tempC=hm.get(last)+1;
                            hm.put(last, tempC);
                        }else{
                            String projtemp=id.get(temp);
                            if(projtemp.equals(last)||projtemp.equals("Banned")){
                                continue;
                            }
                            int projDec=hm.get(projtemp)-1;
                            hm.put(projtemp, projDec);
                            id.put(temp,"Banned");
                        }
                    }
                    if(temp.charAt(0)>='A' &&temp.charAt(0)<='Z'){
                        if(!hm.containsKey(temp)){
                            hm.put(temp, 0);
                            prj.add(temp);
                        }
                        last=temp;
                    }
                }
            if(!flag){
                break;
            }
            LinkedList<Project> list=new LinkedList<Project>();
            while(!prj.isEmpty()){
                String projN=prj.remove();
                int tempC=hm.get(projN);
                list.add(new Project(projN, tempC));
            }
            Collections.sort(list);
            while(!list.isEmpty()){
                Project temp=list.removeLast();
                sb.append(temp.getName()).append(" ").append(temp.getCounter()).append("\n");
            }
        }
        System.out.print(sb);
    }
}
class Project implements Comparable<Project>{
    private String name;
    private int counter;

    public Project(String name, int counter) {
        this.name = name;
        this.counter = counter;
    }

    public int getCounter() {
        return counter;
    }

    public String getName() {
        return name;
    }
   
    @Override
    public int compareTo(Project o) {
        if(this.counter>o.getCounter()){
            return 1;
        }
        if(this.counter<o.getCounter()){
            return -1;
        }else{
            if(this.name.compareTo(o.getName())>-1){
                return -1;
            }else{
                return 1;
            }
        }
    }
}

No comments:

Post a Comment