Pages

1/19/2014

UVA 10008 Cryptanalysis Solution


import java.io.IOException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.PriorityQueue;
import java.util.Set;
import java.util.TreeMap;


class Main {

/**
* @param args
* @throws IOException
*/
@SuppressWarnings("deprecation")
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
boolean flag = false;
String m = null;
StringBuffer res = new StringBuffer("");

Map<Character,Integer> m1 = new TreeMap<>();
Map<Character,Integer> m2 = new TreeMap<>();
int tc = Integer.parseInt(br.readLine());
int t =0;
for (int i =1 ; i <=tc ; i++) {
char ch[]= br.readLine().toUpperCase().replaceAll("[^A-Z]", "").toCharArray();
for (int k =0; k<ch.length; k++ ) {
t=1;
if (m1.containsKey(ch[k])) {
t = m1.get(ch[k]);
t++;

}
m1.put(ch[k], t);
}

}
TreeMap<Character,Integer> sortedData = new TreeMap(new ValueComparer(m1));
sortedData.putAll(m1);

Set it = sortedData.entrySet();
Iterator  ijh = sortedData.entrySet().iterator();
while(ijh.hasNext()) {
Map.Entry<Character, Integer>nnn = (Entry<Character, Integer>) ijh.next();
System.out.println(nnn.getKey() + " " + nnn.getValue());
}


// ArrayList<Character,Integer> arr = new ArrayList <> ();
//System.out.println(res);
}
}
class ValueComparer implements Comparator {
public Map  _data = null;
public ValueComparer (Map<Character, Integer> m1){
super();
_data = m1;
}

    public int compare(Object o1, Object o2) {
    Integer e1 = (Integer) _data.get(o1);
    Integer e2 = (Integer) _data.get(o2);
    int tmp = e2.compareTo(e1);
        if (tmp==0) {
        return ((Character) o1).compareTo((Character) o2);
        }
    return e2.compareTo(e1);
    }
}

No comments :

Post a Comment