Advertisement
Guest User

Untitled

a guest
Nov 12th, 2015
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.60 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. /**
  4.  * Created by mac on 12.11.15.
  5.  */
  6. public class Main {
  7.     public static void main(String[] args) {
  8.         Scanner in = new Scanner(System.in);
  9.  
  10.         Map<String,Integer> map = new HashMap<>();
  11.         String string = in.nextLine();
  12.         int value;
  13.         String key;
  14.         for (int i = 0; i < string.length(); i++) {
  15.             key = "" + string.charAt(i);
  16.             if (map.containsKey(key)) {
  17.                 value = map.get(key) + 1; // получение значения по ключу те частоту
  18.                 map.put(key,value);
  19.             } else {
  20.                 map.put(key,1);
  21.             }
  22.         }
  23.         List<Map.Entry<String, Integer>> list = new ArrayList<>(map.entrySet());
  24.         List<Element> q = new ArrayList<>();
  25.  
  26.         for (int i = 0; i < list.size(); i ++) {
  27.             q.add(new Element(list.get(i).getValue(), list.get(i).getKey()));
  28.         }
  29.  
  30.         Comparator comparator = new Comparator() {
  31.             @Override
  32.             public int compare(Object o1, Object o2) {
  33.                 Element one = (Element) o1;
  34.                 Element two = (Element) o2;
  35.                 return one.p-two.p;
  36.             }
  37.         };
  38.  
  39.         Collections.sort(q,comparator);
  40.         int dl = list.size();
  41.         Element buffer1;
  42.         Element buffer2;
  43.  
  44.         if (list.size() == 1) {
  45.             System.out.println(1 + " " + string.length());
  46.             System.out.println(string.charAt(0) + ": " + "0");
  47.             String simplecode = "";
  48.             for (int i = 0; i < string.length(); i++) {
  49.                 simplecode += "0";
  50.             }
  51.             System.out.println(simplecode);
  52.         } else {
  53.  
  54.             do {
  55.                 buffer1 = extractMin(q);
  56.                 buffer2 = extractMin(q);
  57.                 // добавляем элемент с именем сум двух мин элементов и приоритетом сум двух мин элементов
  58.                 insertElement(q, new Element(buffer1.p + buffer2.p, buffer1.v + buffer2.v, buffer1, buffer2));
  59.             } while (q.size() > 1);
  60.  
  61.             // получаем код 'a'
  62.  
  63.  
  64.             Map<String, String> dictionary = new HashMap<>();
  65.  
  66.             String[] halfresult = new String[q.get(0).v.length()];
  67.  
  68.  
  69.             for (int i = 0; i < q.get(0).v.length(); i++) {
  70.                 String code = "";
  71.                 String symbol = "" + q.get(0).v.charAt(i);
  72.                 Element buffCoding = q.get(0);
  73.                 while ((buffCoding.leftchild != null) && (buffCoding.rightchild != null)) {
  74.                     if (buffCoding.leftchild.v.contains(symbol)) {
  75.                         code += "0";
  76.                         buffCoding = buffCoding.leftchild;
  77.                     } else if (buffCoding.rightchild.v.contains(symbol)) {
  78.                         code += "1";
  79.                         buffCoding = buffCoding.rightchild;
  80.                     }
  81.                 }
  82.                 dictionary.put(symbol, code);
  83.                 halfresult[i] = (symbol + ": " + code);
  84.             }
  85.  
  86.             String resultCode = "";
  87.             for (int i = 0; i < string.length(); i++) {
  88.                 key = "" + string.charAt(i);
  89.                 resultCode += dictionary.get(key);
  90.             }
  91.  
  92.             System.out.println(list.size() + " " + resultCode.length());
  93.  
  94.             for (int i = 0; i < halfresult.length; i ++) {
  95.                 System.out.println(halfresult[i]);
  96.             }
  97.             System.out.println(resultCode);
  98.         }
  99.     }
  100.  
  101.  
  102.     public static class Element{
  103.         int p;
  104.         String v;
  105.         Element leftchild = null;
  106.         Element rightchild = null;
  107.         public Element(int p, String v) {
  108.             this.p = p;
  109.             this.v = v;
  110.         }
  111.  
  112.         public Element(int p, String v, Element leftchild, Element rightchild) {
  113.             this.p = p;
  114.             this.v = v;
  115.             this.leftchild = leftchild;
  116.             this.rightchild = rightchild;
  117.         }
  118.  
  119.     }
  120.  
  121.     public static Element extractMin(List<Element> q) {
  122.         Element buffer = q.get(0);
  123.         for (int i = 1; i < q.size(); i++) {
  124.             q.set(i-1,q.get(i));
  125.         }
  126.         q.remove(q.size()-1);
  127.         return buffer;
  128.     }
  129.  
  130.     public static void insertElement(List<Element> q, Element el) {
  131.         if (q.size() == 0) {
  132.             q.add(el);
  133.             return;
  134.         }
  135.         for (int i = q.size()-1; i >= 0; i--) {
  136.             if (el.p >= q.get(i).p) {
  137.                 q.add(i+1,el);
  138.                 break;
  139.             }
  140.         }
  141.     }
  142.  
  143.  
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement