[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 1708: [Usaco2007 Oct]Money奶牛的硬币( dp )
背包dp.. -------------------------------------------------------------------------------- #include< ...
- Android 开发笔记“程序安装包APK的制作”
资源来源:http://blog.csdn.net/qualcent/article/details/6959547 完成Android项目后,需要将程序打包成APK文件(Android Packag ...
- Qt中Ui名字空间以及setupUi函数的原理和实现 <转>
用最新的QtCreator选择GUI的应用会产生含有如下文件的工程 下面就简单分析下各部分的功能. .pro文件是供qmake使用的文件,不是本文的重点[不过其实也很简单的],在此不多赘述. 所以呢, ...
- .net web初级工程师教程
序 这份教程,只针对正在努力找工作的初级.net web工程师,软件这行,刚入门时找工作是个坎,希望教程对各位有帮助. 教程将通过一个实际项目,简单明了地完整呈现,在实际工作中,工程师都做些什么及怎么 ...
- Linux远程连接与常用命令
要学linux ,一定得用命令界面的,怎么也得是shell语言,用就最难最原始的,用的人都是专家,历史最少也得30年,不管有三七二十一上来就敲ls ,先看看当前目录都有什么.一口专业的linux范儿, ...
- 城通网盘,千军万马,千脑网盘,119g网盘哪个适合做网赚?
转载请注明文章来自 [ofiicexie] 网盘网赚已经流行了有一段时间了,国内流行的几个网盘有城通,千军万马,千脑,119g,今天小编写以此文来比较分析一下这几个网盘的优缺点. 这里,我特意做了个这 ...
- Spring Boot普通类调用bean
1 在Spring Boot可以扫描的包下 假设我们编写的工具类为SpringUtil. 如果我们编写的SpringUtil在Spring Boot可以扫描的包下或者使用@ComponentScan引 ...
- HDU 3328 Flipper
题解:直接建n个栈,模拟过程即可…… #include <cstdio> #include <cstring> #include <stack> using nam ...
- 九款让WordPress成为赚钱利器的广告插件
Blog有了很不错的流量后,看到别人博客挂的广告挣$,是否也有挂广告的冲动,但是,修改wordpress模版去让人不厌其烦,布局.样式都的重新修改一下,为了不那么麻烦,笔者整理的几款wordpress ...
- 编译最新ffmpeg2.0.1到iOS设备
www.mingjianhua.com 转载请注明出处. 上一篇文章讲了用NDKr9编译最新ffmpeg2.0.1到android平台,一般做了Android平台的编解码就免不了要做iOS,这次一起把 ...