Problem 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
 

思路:此题不须要编码,仅仅须要求编码的长度而已,直接乱搞。

#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std; int num[27];
char s[100005]; struct S{
int val; S(int a){val=a;} bool operator<(const S &p) const
{
return val>p.val;
} }; int main()
{
int n,i,len,ans,a,b,cnt; while(~scanf("%s",s))
{
len=strlen(s); if(len==3 && s[0]=='E' && s[1]=='N' && s[2]=='D') return 0; memset(num,0,sizeof num); for(i=0;i<len;i++)
{
if(s[i]!='_') num[s[i]-'A']++;
else num[26]++;
} sort(num,num+27); priority_queue<S>que; cnt=0; for(i=0;i<27;i++) if(num[i])
{
que.push(num[i]); cnt++;
} if(cnt==1)//特判
{
printf("%d %d %.1f\n",len*8,len,8.0); continue;
} ans=0; while(que.size()>1)
{
a=que.top().val;
que.pop();
b=que.top().val;
que.pop(); ans+=a+b; que.push(a+b);
} printf("%d %d %.1f\n",len*8,ans,(double)len*8/ans);
}
}

HDU-1053-Entropy(Huffman编码)的更多相关文章

  1. HDU 1053 Entropy(哈夫曼编码 贪心+优先队列)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1053 Entropy Time Limit: 2000/1000 MS (Java/Others)   ...

  2. hdu 1053 Entropy

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1053 Entropy Description An entropy encoder is a data ...

  3. uvalive 2088 - Entropy(huffman编码)

    题目连接:2088 - Entropy 题目大意:给出一个字符串, 包括A~Z和_, 现在要根据字符出现的频率为他们进行编码,要求编码后字节最小, 然后输出字符均为8字节表示时的总字节数, 以及最小的 ...

  4. hdu 1053 Entropy (哈夫曼树)

    Entropy Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  5. [老文章搬家] 关于 Huffman 编码

    按:去年接手一个项目,涉及到一个一个叫做Mxpeg的非主流视频编码格式,编解码器是厂商以源代码形式提供的,但是可能代码写的不算健壮,以至于我们tcp直连设备很正常,但是经过一个UDP数据分发服务器之后 ...

  6. Huffman编码

    #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <cstdio> #include <cstri ...

  7. 【数据压缩】Huffman编码

    1. 压缩编码概述 数据压缩在日常生活极为常见,平常所用到jpg.mp3均采用数据压缩(采用Huffman编码)以减少占用空间.编码\(C\)是指从字符空间\(A\)到码字表\(X\)的映射.数据压缩 ...

  8. 优先队列求解Huffman编码 c++

    优先队列小析      优先队列的模板: template <class T, class Container = vector<T>,class Compare = less< ...

  9. Huffman编码实现电文的转码与译码

    //first thing:thanks to my teacher---chenrong      Dalian Maritime university /* 构造Huffman Tree思路: ( ...

  10. 【HDOJ】1053 Entropy

    构造huffman编码,果断对字符进行状态压缩. #include <iostream> #include <cstdio> #include <cstring> ...

随机推荐

  1. yzoi1109&&viojs1042最小步数的一点看法——回文数

    Description - 问题描述 有一天,雄霸传授本人风神腿法第一式:捕风捉影..............的步法(弟子一:堂主,你大喘气呀.风:你给我闭嘴.)捕风捉影的关键是换气(换不好就会大喘气 ...

  2. javascript 函数声明问题

    (function(){ //运行正常 test1(); function test1() { console.log('123'); }; })() (function(){ //出错,test2未 ...

  3. Sublime text3 JS语法检测工具安装及使用

    Sublime text3 JS语法检测工具安装及使用 工具/原料 sublime text3 nodejs sublimeLinter sublimeLinter-jshint 方法/步骤 首先ct ...

  4. MBR

    Mbr位于磁盘的0柱面,0磁头,1扇区. MBR       有三部分构成,主引导程序,硬盘分区表DPT和,硬盘的有效标志55AA.在512个字节的主引导扇区里. 主引导程序占446个字节,dpt占6 ...

  5. Android编译输出 资源指定AAPT

    BoardConfigCommon.mk # Default Command lineSEC_DEV_APP_LOCAL_AAPT_FLAGS := -c sw360dpSEC_DEV_FRAMEWO ...

  6. App适配iPhone 6/ Plus和iOS 8:10条小秘诀

    App适配iPhone 6/ Plus和iOS 8:10条小秘诀   iPhone 6iOS 8适配   (原文:raywenderlich 作者:Jack Wu 译者:@TurtleFromMars ...

  7. Ruby自学笔记(二)— Ruby的一些基础知识

    Ruby安装好之后,我们就可以来实践Ruby语言了. 以下是一些学习到的简单基础知识: 1. 如何执行Ruby文件? 我们编写的Ruby文件是以rb为后缀名的,例如:XXX.rb.当要执行ruby文件 ...

  8. inux 下c/c++ 连接mysql数据库全过程-----已经通过验证

    安装Mysql(还可以下载 .gz包,解压安装,再此只介绍此法,其实都一样了,或百度一下...重点在下下下下面) 1.首先安装Mysql服务器 sudo apt-get install mysql-s ...

  9. javascript design patterns

    http://jsdesignpatterns.com/ http://www.joezimjs.com/tag/design-patterns/ http://codecube.net/#archi ...

  10. Unity3d 读取网络xml

    Unity3d 读取网络xml Unity3d 读取网络xml,这个xml文件需要不包含BOM信息,可以用UltraEdit打开xml文件,并且另存为的时候,选择不包含BOM的utf-8格式存储!