Entropy
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 14093 Accepted: 4857

Description

An entropy encoder is a data encoding method that achieves lossless data compression by encoding a message with “wasted” or “extra” information removed. In other words, entropy encoding removes information that was not necessary in the first place to accurately encode the message. A high degree of entropy implies a message with a great deal of wasted information; english text encoded in ASCII is an example of a message type that has very high entropy. Already compressed messages, such as JPEG graphics or ZIP archives, have very little entropy and do not benefit from further attempts at entropy encoding.

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

The input file will contain a list of text strings, one per line. The text strings will consist only of uppercase alphanumeric characters and underscores (which are used in place of spaces). The end of the input will be signalled by a line containing only the word “END” as the text string. This line should not be processed.

Output

For each text string in the input, 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.

Sample Input

AAAAABCD
THE_CAT_IN_THE_HAT
END

Sample Output

64 13 4.9
144 51 2.8

Source

Greater New York 2000

问题链接:POJ1521 LA2088 HDU1053 ZOJ1117 Entropy
问题简述:(略)
问题分析:哈夫曼编码问题,输出3个数,分别是字符串长度、哈夫曼树编码总长度和平均编码长度。用优先队列来处理是方便的。输入的字符串也许比较长,输入缓冲区需要适当大一些。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* POJ1521 LA2088 HDU1053 ZOJ1117 Entropy */#include <iostream>
#include <queue>
#include <cstdio>
#include <cstring>
#include <cctype>using namespace std;const int AL = 26;
int cnt[AL + 1];    /* 统计字符出现的次数 */
char s[1024];int main()
{while (~scanf("%s", s) && strcmp(s, "END") != 0) {memset(cnt, 0, sizeof cnt);for (int i = 0; s[i]; i++)if (isupper(s[i])) cnt[s[i] - 'A']++;else cnt[AL]++;priority_queue<int, vector<int>, greater<int> > q;int len = strlen(s);int ans1 = len * 8, ans2 = 0;for (int i = 0; i <= AL; i++)if(cnt[i]) q.push(cnt[i]);while (q.size() > 1) {int sum = q.top();q.pop();sum += q.top();q.pop();q.push(sum);ans2 += sum;}if (ans2 == 0)ans2 = len;printf("%d %d %.1f\n", ans1, ans2, 1.0 * ans1 / ans2);}return 0;
}

POJ1521 LA2088 HDU1053 ZOJ1117 Entropy【哈夫曼编码】相关推荐

  1. 哈夫曼编码树的经典题目

                                                          点击打开题目链接   poj3253 Fence Repair Time Limit: 20 ...

  2. 信息论与编码_哈夫曼编码

    哈夫曼树 哈夫曼树(Huffman Tree)也是一种特殊的二叉树,这种树的所有叶子结点都带有权值,从中构造出带权路径长度最短的二叉树,即哈夫曼树. 哈夫曼树的定义 ​ 设二叉树具有n个带权值的叶子结 ...

  3. 哈夫曼编码c语言论文,哈夫曼编码的实现及应用论文.doc

    哈夫曼编码的实现及应用论文 毕 业 设 计(论文) 题目 哈夫曼编码的实现 及应用 二级学院 数学与统计学院 专 业 信息与计算科学 班 级 学生姓名 张泽欣 学号 指导教师 职称 时 间 目录 摘要 ...

  4. 用java实现对字符串文本的哈夫曼编码与解码

    哈夫曼树与编码的创建过程及发展由来 这里基础知识就不再叙述了,请参考博客 https://www.cnblogs.com/alomsc/p/12736502.html#:~:text 写的非常详细,初 ...

  5. labview霍夫曼编码_香农编码与霍夫曼编码

    一.香农-范诺编码 香农-范诺(Shannon-Fano)编码的目的是产生具有最小冗余的码词(code word).其基本思想是产生编码长度可变的码词.码词长度可变指的是,被编码的一些消息的符号可以用 ...

  6. c++实现霍夫曼编码

    c++实现霍夫曼编码,计算信源的熵.平均码长.编码效率.冗余度与压缩比 考虑到指针可能对新手不太友好,这里用的是vector容器(用法类似数组,可以动态扩容)存储树形结构,大致原理就是n号结点的左右子 ...

  7. 可逼近信道容量编码技术之霍夫曼编码的实现

    可逼近信道容量编码技术之霍夫曼编码的实现 简介 在当今信息爆炸时代,如何采用有效的数据压缩技术来节省数据文件的存储空间和计算机网络的传送时间已越来越引起人们的重视.哈夫曼编码正是一种应用广泛且非常有效 ...

  8. 数据结构与算法(6-5)二叉树的应用--哈夫曼树与哈夫曼编码

    目录 哈夫曼编码(最优二叉树) 一.优势:缩短电文长度 二.思想: 三.过程: 四.图解实现过程: 五.总代码 哈夫曼编码(最优二叉树) 一.优势:缩短电文长度 二.思想: 获取每个字符出现的频率,用 ...

  9. 哈夫曼树的生成及哈夫曼编码

    首先构造哈夫曼树结构体,初始化哈夫曼树的四个无符号整型域,输入文本,统计各个字符的权值,然后构建哈夫曼树,从根到叶子逆向求哈夫曼树的编码. #include"stdio.h" #i ...

最新文章

  1. Javascript变量函数浅谈
  2. VMware上安装ubuntu 13.04
  3. 记一次使用 android 自带 WebView 做富文本编辑器之API、机型的兼容及各种奇葩bug的解决...
  4. GitHub 回滚操作
  5. JUC锁-LockSupport(四)
  6. 实例学习SSIS(五)--理论介绍SSIS
  7. python prt_Python中的self
  8. 5101是多大的电阻_贴片电阻1206-5101(5.1K) 1206
  9. Latex入门简明教程
  10. matlab开环传递函数 求单位负反馈的系统传递函数,已知单位反馈系统开环传递函数.doc...
  11. 企业网站建设如何选择企业cms系统
  12. 【bzoj3505】 Cqoi2014—数三角形
  13. Android开发之智能聊天机器人
  14. 2021年安全生产模拟考试(全国特种作业操作证电工作业-电气试验模拟考试题库二)安考星
  15. 商业化广告--体系学习-- 7 -- 行业蓝图篇 --广告产品发展路径
  16. xiaopiu原型设计 记录
  17. 【操作系统】哲学家进餐问题
  18. crh寄存器_STM32的寄存器控制SDA_IN()/SDA_OUT()
  19. 软件测试公司都会查学历吗,高新技术企业申请会查员工学历吗?申请高新技术企业注意事项解读...
  20. 如何看待996现象,996工作模式是种什么样的体验?

热门文章

  1. 2018-03-01
  2. 全站仪和手机连接软件_全站仪各方面应用的原理、操作及计算,看这篇就对了...
  3. ArcGIS Maritime Server 开发教程(四)Maritime Service 开发实践
  4. matlab imagesc clims,imagesc
  5. 【java学习之路】(java框架)002.Git配置及使用
  6. JDK历史版本主要新特性
  7. C++小白课本练习4
  8. 在局域网访问_Jupyter notebook设置局域网访问方法
  9. 计算机应用技术参加文献,面向科技文献的机器翻译(4)-计算机应用技术专业毕业论文.docx...
  10. 菜鸟学asp.net遇到的问题和解决方案