Entropy
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.
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.
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
题意:
本题的字面有点长,但是核心的描述就两段,第二段和第四段。让我们求一个字符串每个字符用8个字节编码的长度(L)和用哈夫曼编码的编码长度(W),
以及压缩率,压缩率= L/W;
思路:
还记得wpl的三种解法吗,我们就用第一种,叶子结点的权值统计为字母出现的次数。 第一个结果直接用字符串长度乘以8即可
(1)所有叶子结点的带权路径长度之和;
WPL=15+25+34+44+54+63+93+73+83+102=173
(2)除根结点外的所有结点的权值之和;
WPL=1+2+3+3+4+5+6+6+9+9+7+8+12+10+18+15+22+33=173
(3)除叶子结点的所有结点的权值之和;
WPL=55+22+33+12+18+15+6+9+3=173
坑点1:特殊判断如果全是一个字符的字符串,因为你会发现进入不了求wpl的过程
坑点2:本题是多组输入,而每组测试样例的队列都没有将哈夫曼树的根节点删除。
建立良好的书写代码习惯不管是否是多组数据都应该删除一下。
AC代码
#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
priority_queue<int, vector<int>,greater<int> > q;
int main(){
string s;
int arr[30];
while(cin>>s){
if(s == "END") break;
memset(arr,0,sizeof(arr));
int wpl=0;
for(int i=0;i<s.size();i++){
if(s[i] == '_') arr[26]++;
else arr[s[i]-'A']++;
}
for(int i=0;i<27;i++) if(arr[i]!=0) q.push(arr[i]);
if(q.size()==1){
q.pop();
printf("%d %d %0.1f\n",s.size()*8,q.top(),(double)s.size()*8/q.top());
continue;
}
while(q.size()>1){
int min1=q.top(); q.pop();
int min2=q.top(); q.pop();
wpl+=min1+min2;
q.push(min1+min2);
}
q.pop();
printf("%d %d %0.1f\n",s.size()*8,wpl,(double)s.size()*8/wpl);
}
return 0;
}