| |||||
| |||||
Description
In computer science and information theory, a Huffman code is an optimal prefix code algorithm. In this exercise, please use Huffman coding to encode a given data. You should output the number of bits, denoted asB(T), to encode the data: B(T)=∑f(c)dT(c), wheref(c) is the frequency of character c, and dT(c) is be the depth of characterc's leaf in the tree T.
Input
The first line is the number of characters n. The following n lines, each line gives the character c and its frequencyf(c).
Output
Output a single number B(T).
Sample Input
5 0 5 1 4 2 6 3 2 4 3
Sample Output
45
Hint
0: 01
1: 00
2: 11
3: 100
4: 101
题目的意思大致是(直接看测试样例)第一行为测试样例数,接下来n行,第一个为字符(不一定是整数的),第二个是字符出现的频率。 此题的一种哈夫曼树形式如下(构造过程见另一篇博客) (从第0层开始)WPL = 2 * 3(长度为3的编码形式)+3*3 + (4+5+6)*2 = 45; 这是定义的计算WPL的方式,然后我们看一下另一个奇妙的结果20 + 9 + 11 + 5 = 45; 证明如下:(证明并不充分 详细点的证明和讲解) 如果知道了这个结论,这种题目的难度完全降没了。接下来就贴代码了。代码太渣 ![]()
另外还可以用优先队列(用小根堆的),这样就只需要每次比较对首两个元素。 |