Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Лапшева консольный
- import java.util.*;
- public class Solution {
- static ArrayList<Character> code = new ArrayList<>();
- static TreeMap<Character, ArrayList<Character>> table = new TreeMap<>();
- public static class Token implements Comparable<Token> {
- String information;
- int weight;
- boolean isAtom;
- Token() {
- information = new String();
- weight = -1;
- isAtom = true;
- }
- Token(Character what, int weight) {
- information = new String(what.toString());
- this.weight = weight;
- isAtom = true;
- }
- Token(String what, int weight) {
- information = what;
- this.weight = weight;
- isAtom = false;
- }
- Token add(Token other) {
- Token ans = new Token();
- ans.information = new String(information + other.information);
- ans.weight = weight + other.weight;
- ans.isAtom = false;
- return ans;
- }
- @Override
- public int compareTo(Token rhs) {
- if (weight != rhs.weight) {
- return weight - rhs.weight;
- }
- if (isAtom != rhs.isAtom) {
- if (isAtom)
- return -1;
- return 1;
- }
- return information.compareTo(rhs.information);
- }
- }
- public static class Tree implements Comparable<Tree> {
- Token token;
- Tree left;
- Tree right;
- Tree() {
- token = new Token();
- left = null;
- right = null;
- }
- Tree(Token token) {
- this.token = token;
- left = null;
- right = null;
- }
- Tree(Token token, Tree left, Tree right) {
- this.token = token;
- this.left = left;
- this.right = right;
- }
- @Override
- public int compareTo(Tree rhs) {
- return token.compareTo(rhs.token);
- }
- }
- public static void buildTable(Tree root) {
- if (root.left != null) {
- code.add('0');
- buildTable(root.left);
- }
- if (root.right != null) {
- code.add('1');
- buildTable(root.right);
- }
- if (root.token.isAtom) {
- var list = new ArrayList<Character>(code);
- table.put(root.token.information.charAt(0), list);
- }
- if (!code.isEmpty())
- code.remove(code.size() - 1);
- }
- public static void main(String[] args) throws Exception {
- var s = new Scanner(System.in);
- String input = s.nextLine();
- var chars = new TreeMap<Character, Integer>();
- for (int i = 0; i < input.length(); ++i) {
- var cur = input.charAt(i);
- if (chars.containsKey(cur)) {
- chars.put(cur, chars.get(cur) + 1);
- } else {
- chars.put(cur, 1);
- }
- }
- var tree = new PriorityQueue<Tree>();
- for (var i : chars.entrySet()) {
- var newToken = new Token(i.getKey(), i.getValue());
- var newTree = new Tree(newToken);
- tree.add(newTree);
- }
- System.out.println("Символы (" + chars.size() + " уникальных):");
- for (var i : chars.entrySet()) {
- System.out.println(i.getKey() + "(" + i.getValue() + ")");
- }
- System.out.println();
- System.out.println("Листья (вес, алфавит, атомарность): ");
- var tmp = new PriorityQueue<Tree>(tree);
- while (!tmp.isEmpty()) {
- var cur = tmp.poll();
- System.out.println(cur.token.information + "(" + cur.token.weight + ")");
- }
- System.out.println();
- int step = 1;
- while (tree.size() > 1) {
- var left = tree.poll();
- var right = tree.poll();
- var parentToken = left.token.add(right.token);
- var parentTree = new Tree(parentToken, left, right);
- tree.add(parentTree);
- System.out.print("Шаг " + step + ": ");
- System.out.print(left.token.information + "(" + left.token.weight + ") + ");
- System.out.print(right.token.information + "(" + right.token.weight + ") = ");
- System.out.println(parentTree.token.information + "(" +
- parentTree.token.weight + ")");
- tmp = new PriorityQueue<Tree>(tree);
- while (!tmp.isEmpty()) {
- var cur = tmp.poll();
- System.out.println(cur.token.information + "(" + cur.token.weight + ")");
- }
- System.out.println();
- ++step;
- }
- System.out.println();
- Solution.buildTable(tree.peek());
- System.out.print("Длина строки: " + input.length() + "\n");
- int power = (int) Math.ceil(Math.log(chars.size()) / Math.log(2));
- int badSize = power * input.length();
- System.out.print("i: " + power + "\n");
- System.out.print("Объем по Хартли: " + badSize + "\n");
- System.out.print("Объем по Хаффману: ");
- Integer goodSize = 0;
- for (var i : table.entrySet()) {
- goodSize += chars.get(i.getKey()) * i.getValue().size();
- }
- System.out.print(goodSize.toString() + "\n");
- double answer = 1.0 * badSize / goodSize;
- System.out.print("Ответ (Vхартли / Vхаффман): " + answer + "\n\n");
- for (var i : table.entrySet()) {
- System.out.print(i.getKey().toString() + ": ");
- for (var j : i.getValue()) {
- System.out.print(j.toString());
- }
- System.out.print("\n");
- }
- s.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement