HDU 1053 && PKU 1521 Entropy (PKU 3253, HDU 2527同解)

来源:互联网 发布:淘宝丝芙兰68折代购 编辑:程序博客网 时间:2024/06/10 02:35

Huffman编码, 题意:给你一个序列,output the length in bits of the 8-bit ASCII encoding, the length in bits of an optimal prefix-free variable-length encoding, and the compression ratio accurate to one decimal point.

 

其实就是要你构造最优前缀树, 也就是Huffman树, 求它的带权路径和。 刚开始每种字符代表一个根节点, 它出现的次数就是其权值, 接下来就是求构造的Huffman树的带权路径和了。

 

刚开始比较傻, 先建立了Huffman树的二叉链表,然后通过先序遍历求它的带权路径和。

 

code:

24963902010-05-28 12:34:39Accepted10530MS256K1670 BG++xwc-hdu

 

后来发现不用建树的, 因为是求树的带权路径长度, 根据权值可以直接求得。

 

 

 

原创粉丝点击