[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 ...
随机推荐
- 5.6.2 Number类型
Number是与数字对应的引用类型.要创建Number对象,可以在调用Number构造函数时向其中传递相应的数值.例如: var numberObject=new Number(10); 与Boole ...
- PHP获取网址的PR值
PR值是google衡量网站的重要标准之一,根据google提供的结果获取pr值,如:http://toolbarqueries.google.com.hk/tbr?client=navclient- ...
- awk的用法(转)
awk 用法:awk ' pattern {action} ' 变量名 含义 ARGC 命令行变元个数 ARGV 命令行变元数组 FILENAME 当前输入文件名 FNR 当前文件中的记录号 FS 输 ...
- python10min系列之面试题解析:python实现tail -f功能
同步发布在github上,跪求star 这篇文章最初是因为reboot的群里,有人去面试,笔试题有这个题,不知道怎么做,什么思路,就发群里大家讨论 我想了一下,简单说一下我的想法吧,当然,也有很好用的 ...
- java时间验证工具
可以验证2014-02-21这种错误
- net-snmp启用python模块
1. http://blog.jeoygin.org/2011/09/centos-5-install-python-2-6-and-python-setuptools-2-6.html 2. htt ...
- (C#)Windows Shell 编程系列4 - 上下文菜单(iContextMenu)(二)嵌入菜单和执行命令
原文(C#)Windows Shell 编程系列4 - 上下文菜单(iContextMenu)(二)嵌入菜单和执行命令 (本系列文章由柠檬的(lc_mtt)原创,转载请注明出处,谢谢-) 接上一节:( ...
- Swift语言iOS开发:CALayer十则示例
如你所知,我们在iOS应用中看到的都是视图(view),包括按钮视图.表视图.滑动条视图,还有可以容纳其他视图的父视图等. AD:[活动]Web和APP兼容性实战 Win10训练营免费报名 如你所知, ...
- sql日期转换格式
Select CONVERT(varchar(100), GETDATE(), 0): 05 16 2006 10:57AM Select CONVERT(varchar(100), GETDATE( ...
- Java多线程之synchronized(五)
上篇介绍了用synchronized修饰static方式来实现“Class 锁”,今天要介绍另一种实现方式,synchronized(class)代码块,写法不一样但是作用是一样的.下面我附上一段代码 ...