[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 ...
随机推荐
- BZOJ 3444: 最后的晚餐( )
把暗恋关系看成无向边, 那某个点度数超过2就无解.存在环也是无解.有解的话对连通分量进行排列就行了. ------------------------------------------------- ...
- USACO Section 5.3 Milk Measuring (IDDFS+dp)
迭代加深搜索,从小到大枚举桶数的上限maxd:对每个maxd,枚举每个组合,判断是否能够倒出q:直到得到answer.判断的部分就用dp(完全背包). ------------------------ ...
- 加装 ImageMagick 性能更佳!
1. 下载 Download ImageMagick 以此文件ImageMagick-6.9.1-10-Q16-x64-dll-win进行,第二次开发的研发 2. 安装 Install ImageMa ...
- selenium 学习笔记 ---新手学习记录(8) 问题总结(java)
1.获取执行js代码后的返回值 //获取滚动距离 String jl="return $('#chapterul li').height();"; Long jlhq=(Long) ...
- selenium 学习笔记 ---新手学习记录(5) 问题总结(java)
1.今天遇到个奇葩问题,iframe有两个id相同的(如下图) 使用driver.switchTo().frame(“frmLinkPage1”);这个无法使用了. 后来改用driver.switch ...
- Cubieboard 关闭板载led
修改script.bin 找到最后节点[led_para] 修改leds_used = 0 script.bin 一般在系统盘的第一个分区 例如nand就在/dev/nanda sdcard就在/d ...
- Sublime 编辑器主题
Sublime主题分为两种 一种是编辑框中的代码的颜色 另一种是编辑器本身的颜色(不只是颜色哟 Sublime编辑器左边侧边栏的字很小对不对 !有了主题就可以改) 这个主题叫做Soda http ...
- MFC模式对话框与非模式对话框 消息处理顺序
对话框有两种创建方式:DoModal和Creat. 其中DoModal创建的是模态的对话框,而Creat创建的是非模态的对话框下面总结下他们的不同. 对于模态的对话框,在该对话框被关闭前,用户将不能在 ...
- Struts2 中action之间的跳转(分享)
例如从你的login.action到register.action 有两种实现方式 1. 设置type="redirect" <package name="st ...
- The Longest Straight(二分,离散化)
Problem 2216 The Longest Straight Accept: 7 Submit: 14 Time Limit: 1000 mSec Memory Limit : 3 ...