(UVA) 10062 - Tell me the frequencies! - Solução

import java.io.*;
import java.util.*;
import java.lang.Math;

class Main {
    public static void main(String[] args) throws NumberFormatException, IOException {
        Main processando = new Main();
        processando.takeInput();
       
        System.exit(0);
    }
   
    void takeInput() throws NumberFormatException, IOException {
        String line = "";

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
       
        Map<Integer, Character> ascii = new TreeMap<Integer, Character>();
       
        ascii.put(32, ' ');
        ascii.put(33, '!');
        ascii.put(34, '"');
        ascii.put(35, '#');
        ascii.put(36, '$');
        ascii.put(37, '%');
        ascii.put(38, '&');
        ascii.put(39, '\'');
        ascii.put(40, '(');
        ascii.put(41, ')');
        ascii.put(42, '*');
        ascii.put(43, '+');
        ascii.put(44, ',');
        ascii.put(45, '-');
        ascii.put(46, '.');
        ascii.put(47, '/');
        ascii.put(48, '0');
        ascii.put(49, '1');
        ascii.put(50, '2');
        ascii.put(51, '3');
        ascii.put(52, '4');
        ascii.put(53, '5');
        ascii.put(54, '6');
        ascii.put(55, '7');
        ascii.put(56, '8');
        ascii.put(57, '9');
        ascii.put(58, ':');
        ascii.put(59, ';');
        ascii.put(60, '<');
        ascii.put(61, '=');
        ascii.put(62, '>');
        ascii.put(63, '?');
        ascii.put(64, '@');
        ascii.put(65, 'A');
        ascii.put(66, 'B');
        ascii.put(67, 'C');
        ascii.put(68, 'D');
        ascii.put(69, 'E');
        ascii.put(70, 'F');
        ascii.put(71, 'G');
        ascii.put(72, 'H');
        ascii.put(73, 'I');
        ascii.put(74, 'J');
        ascii.put(75, 'K');
        ascii.put(76, 'L');
        ascii.put(77, 'M');
        ascii.put(78, 'N');
        ascii.put(79, 'O');
        ascii.put(80, 'P');
        ascii.put(81, 'Q');
        ascii.put(82, 'R');
        ascii.put(83, 'S');
        ascii.put(84, 'T');
        ascii.put(85, 'U');
        ascii.put(86, 'V');
        ascii.put(87, 'W');
        ascii.put(88, 'X');
        ascii.put(89, 'Y');
        ascii.put(90, 'Z');
        ascii.put(91, '[');
        ascii.put(92, '\\');
        ascii.put(93, ']');
        ascii.put(94, '^');
        ascii.put(95, '_');
        ascii.put(96, '`');
        ascii.put(97, 'a');
        ascii.put(98, 'b');
        ascii.put(99, 'c');
        ascii.put(100, 'd');
        ascii.put(101, 'e');
        ascii.put(102, 'f');
        ascii.put(103, 'g');
        ascii.put(104, 'h');
        ascii.put(105, 'i');
        ascii.put(106, 'j');
        ascii.put(107, 'k');
        ascii.put(108, 'l');
        ascii.put(109, 'm');
        ascii.put(110, 'n');
        ascii.put(111, 'o');
        ascii.put(112, 'p');
        ascii.put(113, 'q');
        ascii.put(114, 'r');
        ascii.put(115, 's');
        ascii.put(116, 't');
        ascii.put(117, 'u');
        ascii.put(118, 'v');
        ascii.put(119, 'w');
        ascii.put(120, 'x');
        ascii.put(121, 'y');
        ascii.put(122, 'z');
        ascii.put(123, '{');
        ascii.put(124, '|');
        ascii.put(125, '}');
        ascii.put(126, '~');
        ascii.put(127, (char)127);
       
        Integer vez = 1;
       
        while ((line = br.readLine()) != null) {
       
            if (vez != 1) {  
               System.out.println();
            }
            vez++;
           
            Integer tamanho;       
       
            ArrayList<Character> arrayLinha = new ArrayList<Character>();
           
            tamanho = line.length();
            Integer i;
            Integer j;
            Integer index = 0;
           
            for (i = 0; i < tamanho; i++) {
                arrayLinha.add(index, line.charAt(i));
                index++;
            }
           
            Map<Integer, Integer> frequencia = new TreeMap<Integer, Integer>();
           
            tamanho = arrayLinha.size();
            Integer contador;
           
            for (i = 0; i < tamanho; i++) {
                for (j = 32; j <= 128; j++) {
                    if (arrayLinha.get(i) == ascii.get(j)) {
                        if (frequencia.get(j) == null || frequencia.get(j) == 0) {
                            frequencia.put(j, 1);
                        }
                        else {
                            contador = frequencia.get(j);
                            contador++;
                            frequencia.put(j, contador);
                        }
                        break;
                    }
                }
            }
           
            frequencia = sortByComparator(frequencia);
           
            tamanho = frequencia.size();
           
            ArrayList<Integer> key = new ArrayList<Integer>();
            ArrayList<Integer> value = new ArrayList<Integer>();
           
            index = 0;       
            for (Map.Entry<Integer, Integer> chave: frequencia.entrySet()) {
                key.add(index, chave.getKey());
                value.add(index, chave.getValue());
                index++;
            }
           
            for (i = 0; i < tamanho; i++) {
                if (i < tamanho-1 && value.get(i) == value.get(i+1)) {
                    i = novoOrdena(key, value, i, tamanho);
                }
                else {
                    System.out.println(key.get(i) + " " + value.get(i));
                }
            }
           
        }
             
        return;
    }

    private static Integer novoOrdena(ArrayList<Integer> key, ArrayList<Integer> value, Integer i, Integer tamanho) {
        Integer j = i;
        Integer contador = i;
       
        while (j < tamanho-1 && value.get(j) == value.get(j+1)) {
            contador++;
            j++;
        }
       
        for (j = contador; j >= i; j--) {
            System.out.println(key.get(j) + " " + value.get(j));
        }
       
        return contador;
           
    }
   
    // ordena pelo value do map
    private static Map sortByComparator(Map unsortMap) {

        List list = new LinkedList(unsortMap.entrySet());

        // sort list based on comparator
        Collections.sort(list, new Comparator() {
            public int compare(Object o1, Object o2) {
                return ((Comparable) ((Map.Entry) (o1)).getValue())
                                       .compareTo(((Map.Entry) (o2)).getValue());
            }
        });

        // put sorted list into map again
                //LinkedHashMap make sure order in which keys were inserted
        Map sortedMap = new LinkedHashMap();
        for (Iterator it = list.iterator(); it.hasNext();) {
            Map.Entry entry = (Map.Entry) it.next();
            sortedMap.put(entry.getKey(), entry.getValue());
        }
        return sortedMap;
    }
}

Comments

Popular posts from this blog

(Coderbyte) Dash Insert II - Solução

(Coderbyte) Run Length - Solução

(Coderbyte) Counting Minutes I - Solução