[POJ 1521]--Entropy(哈夫曼树)
题目链接:http://poj.org/problem?id=1521
| Time Limit: 1000MS | Memory Limit: 10000K |
Description
English text encoded in ASCII has a high degree of entropy because all characters are encoded using the same number of bits, eight. It is a known fact that the letters E, L, N, R, S and T occur at a considerably higher frequency than do most other letters in english text. If a way could be found to encode just these letters with four bits, then the new encoding would be smaller, would contain all the original information, and would have less entropy. ASCII uses a fixed number of bits for a reason, however: it’s easy, since one is always dealing with a fixed number of bits to represent each possible glyph or character. How would an encoding scheme that used four bits for the above letters be able to distinguish between the four-bit codes and eight-bit codes? This seemingly difficult problem is solved using what is known as a "prefix-free variable-length" encoding.
In such an encoding, any number of bits can be used to represent any glyph, and glyphs not present in the message are simply not encoded. However, in order to be able to recover the information, no bit pattern that encodes a glyph is allowed to be the prefix of any other encoding bit pattern. This allows the encoded bitstream to be read bit by bit, and whenever a set of bits is encountered that represents a glyph, that glyph can be decoded. If the prefix-free constraint was not enforced, then such a decoding would be impossible.
Consider the text "AAAAABCD". Using ASCII, encoding this would require 64 bits. If, instead, we encode "A" with the bit pattern "00", "B" with "01", "C" with "10", and "D" with "11" then we can encode this text in only 16 bits; the resulting bit pattern would be "0000000000011011". This is still a fixed-length encoding, however; we’re using two bits per glyph instead of eight. Since the glyph "A" occurs with greater frequency, could we do better by encoding it with fewer bits? In fact we can, but in order to maintain a prefix-free encoding, some of the other bit patterns will become longer than two bits. An optimal encoding is to encode "A" with "0", "B" with "10", "C" with "110", and "D" with "111". (This is clearly not the only optimal encoding, as it is obvious that the encodings for B, C and D could be interchanged freely for any given encoding without increasing the size of the final encoded message.) Using this encoding, the message encodes in only 13 bits to "0000010110111", a compression ratio of 4.9 to 1 (that is, each bit in the final encoded message represents as much information as did 4.9 bits in the original encoding). Read through this bit pattern from left to right and you’ll see that the prefix-free encoding makes it simple to decode this into the original text even though the codes have varying bit lengths.
As a second example, consider the text "THE CAT IN THE HAT". In this text, the letter "T" and the space character both occur with the highest frequency, so they will clearly have the shortest encoding bit patterns in an optimal encoding. The letters "C", "I’ and "N" only occur once, however, so they will have the longest codes.
There are many possible sets of prefix-free variable-length bit patterns that would yield the optimal encoding, that is, that would allow the text to be encoded in the fewest number of bits. One such optimal encoding is to encode spaces with "00", "A" with "100", "C" with "1110", "E" with "1111", "H" with "110", "I" with "1010", "N" with "1011" and "T" with "01". The optimal encoding therefore requires only 51 bits compared to the 144 that would be necessary to encode the message with 8-bit ASCII encoding, a compression ratio of 2.8 to 1.
Input
Output
Sample Input
AAAAABCD
THE_CAT_IN_THE_HAT
END
Sample Output
64 13 4.9
144 51 2.8
Source
#include <iostream>
#include <algorithm>
#include <string>
#include <iomanip>//C++控制输出
using namespace std;
typedef struct{
char key;
int weight, parent, lchild, rchild;
}Huffman;
typedef struct{
int start;
char key[];
}HuffmanCode;
void CreatHuffman(Huffman *ht, int n){
int left, right, lnode, rnode, i, k;
for (i = ; i < * n - ; i++)
ht[i].parent = ht[i].lchild = ht[i].rchild = -;
for (i = n; i < * n - ; i++){
left = right = 0x7fffffff;
lnode = rnode = -;
for (k = ; k < i; k++){
if (ht[k].parent == -){
if (ht[k].weight < left){
right = left;
rnode = lnode;
left = ht[k].weight;
lnode = k;
}
else if (ht[k].weight < right){
right = ht[k].weight;
rnode = k;
}
}
}
ht[i].weight = ht[lnode].weight + ht[rnode].weight;
ht[i].lchild = lnode;
ht[i].rchild = rnode;
ht[lnode].parent = ht[rnode].parent = i;
}
}
void CreatKey(Huffman *ht, HuffmanCode *hcd, int n, int &len){
int i, father, code;
for (i = ; i < n; i++){
code = i;
father = ht[code].parent;
hcd[i].start = n;
while (father != -){
if (ht[father].lchild == code)
hcd[i].key[hcd[i].start--] = '';
else
hcd[i].key[hcd[i].start--] = '';
code = father;
father = ht[code].parent;
}
hcd[i].start++;
len += ht[i].weight*(n - hcd[i].start + );
}
}
int main(){
string keys;
int i, cnt, k, len, L;
while (cin >> keys, keys != "END"){
Huffman ht[];
HuffmanCode hcd[];
L = keys.size(), cnt = , k = len = ;
sort(keys.begin(), keys.end());
ht[].key = keys[];
for (i = ; i < L; i++){
if (keys[i] != keys[i - ]){
ht[k++].weight = cnt;
ht[k].key = keys[i];
cnt = ;
}
else cnt++;
}
ht[k++].weight = cnt;
//只有一种字符,直接输出,建树会出错
if (k == ){
cout << L * << ' ' << L << " 8.0\n";
continue;
}
CreatHuffman(ht, k);
CreatKey(ht, hcd, k, len);
cout << L * << ' ' << len << ' ' << setiosflags(ios::fixed) << setprecision() << L*8.0 / (len*1.0) << endl;
}
return ;
}
这道题学校OJ (Swust OJ)也有,链接在这:http://acm.swust.edu.cn/problem/338/
[POJ 1521]--Entropy(哈夫曼树)的更多相关文章
- PKU 1521 Entropy(简单哈弗曼树_水过)
题目大意:原题链接 给你一个字符串,首先是计算出一个按正常编码的编码长度,其次是计算出一个用霍夫曼编码的编码长度,最后求正常编码的长度除以霍夫曼编码长度的比值,保留一位小数. 解题思路:需要知道 1. ...
- hdu 1053 Entropy (哈夫曼树)
Entropy Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- poj 3253 Fence Repair(优先队列+哈夫曼树)
题目地址:POJ 3253 哈夫曼树的结构就是一个二叉树,每个父节点都是两个子节点的和. 这个题就是能够从子节点向根节点推. 每次选择两个最小的进行合并.将合并后的值继续加进优先队列中.直至还剩下一个 ...
- POJ 3253 Fence Repair(优先队列,哈夫曼树,模拟)
题目 //做哈夫曼树时,可以用优先队列(误?) //这道题教我们优先队列的一个用法:取前n个数(最大的或者最小的) //哈夫曼树 //64位 //超时->优先队列,,,, //这道题的优先队列用 ...
- Poj 3253 Fence Repair(哈夫曼树)
Description Farmer John wants to repair a small length of the fence around the pasture. He measures ...
- POJ 3253 Fence Repair(哈夫曼树)
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 26167 Accepted: 8459 Des ...
- 哈夫曼树---POJ3253
http://poj.org/problem?id=3253 这就是 最典型的哈夫曼树的题型,我们就根据这道题学习一下哈夫曼树 这是最开始我们把21据下来之后我们据下8,然后再据下5得到34,可以看出 ...
- BZOJ 3253 Fence Repair 哈夫曼树 水题
http://poj.org/problem?id=3253 这道题约等于合并果子,但是通过这道题能够看出来哈夫曼树是什么了. #include<cstdio> #include<c ...
- poj3253 Fence Repair【哈夫曼树+优先队列】
Description Farmer John wants to repair a small length of the fence around the pasture. He measures ...
随机推荐
- 我用过的Linux命令--虚拟机和宿主机的网络连接方式
VMWare提供了三种工作模式,它们是bridged(bridged模式:对应网卡vment0).NAT(网络地址转换模式:对应网卡vment8)和host-only(主机模式:对应网卡vment1) ...
- 简单十步让你全面理解SQL
很多程序员认为SQL是一头难以驯服的野兽.它是为数不多的声明性语言之一,也因为这样,其展示了完全不同于其他的表现形式.命令式语言. 面向对象语言甚至函数式编程语言(虽然有些人觉得SQL 还是有些类似功 ...
- java 去除数组重复数据,并输出重复数据值
/** * 去除重复数据 * @author Sunqinbo */ public class RemoveDuplicateData { public static void main(String ...
- Python开发环境Spyder安装方法
Spyder(Scientific PYthon Development EnviRonment)是一个强大的交互式 Python 语言开发环境,提供高级的代码编辑.交互测试.调试等特性,支持包括 W ...
- [LeetCode]题解(python):099-Recover Binary Search Tree
题目来源: https://leetcode.com/problems/recover-binary-search-tree/ 题意分析: 二叉搜索树中有两个点错了位置,恢复这棵树. 题目思路: 如果 ...
- jQuery File Upload
jQuery File Upload介绍.............................................. 2 实现基本原理......................... ...
- 转: js中的getYear()函数的问题(推荐用 getFullYear())
用了JS的getYear()方法,但是发现生成的代码竟然有108(本应该是2008),发现这是firefox下的问题. 然后google了一下,发 现原来是这样的:var today = new da ...
- 自定义标签(TagSupport )
转载:http://zhuhuide2004.iteye.com/blog/555737 这个图太好了,拿下来,标注一下:
- 手工部署Sqlserver CLR程序集
原文 手工部署Sqlserver CLR程序集 以前一直用VS部署Sqlserver CLR程序集简单省事,现在服务器部署在内网了,必须手动更新部署Sqlserver CLR程序集. 开始以为A ...
- CMake 教程
CMake是一个跨平台的程序构建工具,比如起自己编写Makefile方便很多. 介绍:http://baike.baidu.com/view/1126160.htm 本文件不介绍CMake的基本语法, ...