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:
2496390 | 2010-05-28 12:34:39 | Accepted | 1053 | 0MS | 256K | 1670 B | G++ | xwc-hdu |
后来发现不用建树的, 因为是求树的带权路径长度, 根据权值可以直接求得。