Processing math: 100%
祝同学们学习进步,编程快乐!
Problem 3172 --15_D : Huffman Coding

3172: 15_D : Huffman Coding

"
Time Limit 1 秒/Second(s) Memory Limit 512 兆字节/Megabyte(s)
提交总数 0 正确数量 0
裁判形式 标准裁判/Standard Judge 我的状态 尚未尝试
难度 分类标签

We want to encode a given string SS to a binary string. Each alphabet character in SS should be mapped to a different variable-length code and the code must not be a prefix of others.

Huffman coding is known as one of ways to obtain a code table for such encoding.

For example, we consider that appearance frequencycies of each alphabet character in SS are as follows:

a b c d e f
freq. 45 13 12 16 9 5
code 0 101 100 111 1101 1100

We can obtain "Huffman codes" (the third row of the table) using Huffman's algorithm.

The algorithm finds a full binary tree called "Huffman tree" as shown in the following figure.

To obtain a Huffman tree, for each alphabet character, we first prepare a node which has no parents and a frequency (weight) of the alphabet character. Then, we repeat the following steps:

  1. Choose two nodes, xx and yy, which has no parents and the smallest weights.
  2. Create a new node zz whose weight is the sum of xx's and yy's.
  3. Add edge linking zz to xx with label 00 and to yy with label 11. Then zz become their parent.
  4. If there is only one node without a parent, which is the root, finish the algorithm. Otherwise, go to step 1.

Finally, we can find a "Huffman code" in a path from the root to leaves.

Task

Given a string SS, output the length of a binary string obtained by using Huffman coding for SS.

SS

A string SS is given in a line.

Print the length of a binary string in a line.
abca
aaabbcccdeeeffg
z
6
41
1

Constraints

  • 1|S|1051≤|S|≤105
  • SS consists of lowercase English letters.


推荐代码 查看3172 所有题解 上传题解视频得图灵币

本题记录 用 户(点击查看用户) 运行号(点击购买题解) 时 间
算法最快[ ms]
内存最少[ KB]
第一AC
第一挑战

赛题来源/所属竞赛 会津大学《挑战数据结构与算法》 挑战数据结构与算法

竞赛编号 竞赛名称 竞赛时间 访问比赛
AOJ
祝同学们学习进步,编程快乐!