码迷,mamicode.com
首页 > 其他好文 > 详细

Entropy(哈弗曼编码,优先队列)

时间:2015-03-02 09:36:23      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:entropy   哈弗曼编码   优先队列   

<div class="plm" style="text-align: center; font-size: 12pt; color: rgb(34, 34, 34); clear: both;"><div class="ptt" id="problem_title" style="font-size: 18pt; font-weight: bold; color: blue; padding: 10px;">Entropy</div><span id="crawlSuccess" class="crawlInfo" style="display: inline;"><strong>Time Limit:</strong><span id="timeLimit">1000</span>MS     <strong>Memory Limit:</strong><span id="memoryLimit">10000</span>KB     <strong>64bit IO Format:</strong><span id="_64IOFormat">%I64d & %I64u</span></span><div id="problem_opt" style="font-size: 12px; margin-top: 10px;"><a target=_blank id="submit" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" role="button" style="display: inline-block; position: relative; padding: 0px; margin-right: 0.1em; cursor: pointer; vertical-align: middle; overflow: visible; font-family: Verdana, Arial, sans-serif; font-size: 1em; border: 1px solid rgb(211, 211, 211); background-image: url(http://acm.hust.edu.cn/vjudge/jquery-ui-1.11.1.custom/images/ui-bg_glass_75_e3e4f8_1x400.png); background-color: rgb(227, 228, 248); color: rgb(85, 85, 85); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-left-radius: 4px; border-bottom-right-radius: 4px; background-position: 50% 50%; background-repeat: repeat no-repeat;"><span class="ui-button-text" style="display: block; padding: 0.4em 1em;">Submit</span></a> <a target=_blank id="problem_status" href="http://acm.hust.edu.cn/vjudge/contest/view.action?cid=68827#status//E/0" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" role="button" style="display: inline-block; position: relative; padding: 0px; margin-right: 0.1em; cursor: pointer; vertical-align: middle; overflow: visible; text-decoration: none; font-family: Verdana, Arial, sans-serif; font-size: 1em; border: 1px solid rgb(211, 211, 211); background-image: url(http://acm.hust.edu.cn/vjudge/jquery-ui-1.11.1.custom/images/ui-bg_glass_75_e3e4f8_1x400.png); background-color: rgb(227, 228, 248); color: rgb(85, 85, 85); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-left-radius: 4px; border-bottom-right-radius: 4px; background-position: 50% 50%; background-repeat: repeat no-repeat;"><span class="ui-button-text" style="display: block; padding: 0.4em 1em;">Status</span></a></div></div><div style="color: rgb(34, 34, 34); font-family: Verdana, Arial, sans-serif; font-size: 14px; width: 960px; margin: auto;"><div class="hiddable" id="vj_description" style="font-family: 'times new roman'; font-size: 17px;"><p class="pst" style="font-family: Arial, Helvetica, sans-serif; font-size: 18pt; font-weight: bold; color: blue; margin-bottom: 0px;">Description</p><div class="textBG" style="background-color: rgb(234, 235, 255); border-top-left-radius: 10px; border-top-right-radius: 10px; border-bottom-right-radius: 10px; border-bottom-left-radius: 10px; padding: 10px; border-style: dotted; border-width: 2px;"><div class="ptx" lang="en-US" style="font-family: 'Times New Roman', Times, serif; font-size: 12pt;">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. 
</div></div></div><div class="hiddable" id="vj_input" style="font-family: 'times new roman'; font-size: 17px;"><p class="pst" style="font-family: Arial, Helvetica, sans-serif; font-size: 18pt; font-weight: bold; color: blue; margin-bottom: 0px;">Input</p><div class="textBG" style="background-color: rgb(234, 235, 255); border-top-left-radius: 10px; border-top-right-radius: 10px; border-bottom-right-radius: 10px; border-bottom-left-radius: 10px; padding: 10px; border-style: dotted; border-width: 2px;"><div class="ptx" lang="en-US" style="font-family: 'Times New Roman', Times, serif; font-size: 12pt;">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.</div></div></div><div class="hiddable" id="vj_output" style="font-family: 'times new roman'; font-size: 17px;"><p class="pst" style="font-family: Arial, Helvetica, sans-serif; font-size: 18pt; font-weight: bold; color: blue; margin-bottom: 0px;">Output</p><div class="textBG" style="background-color: rgb(234, 235, 255); border-top-left-radius: 10px; border-top-right-radius: 10px; border-bottom-right-radius: 10px; border-bottom-left-radius: 10px; padding: 10px; border-style: dotted; border-width: 2px;"><div class="ptx" lang="en-US" style="font-family: 'Times New Roman', Times, serif; font-size: 12pt;">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.</div></div></div><div class="hiddable" id="vj_sampleInput" style="font-family: 'times new roman'; font-size: 17px;"><p class="pst" style="font-family: Arial, Helvetica, sans-serif; font-size: 18pt; font-weight: bold; color: blue; margin-bottom: 0px;">Sample Input</p><div class="textBG" style="background-color: rgb(234, 235, 255); border-top-left-radius: 10px; border-top-right-radius: 10px; border-bottom-right-radius: 10px; border-bottom-left-radius: 10px; padding: 10px; border-style: dotted; border-width: 2px;"><pre class="sio" style="white-space: pre-wrap; word-wrap: break-word; font-family: 'Courier New', Courier, monospace; font-size: 12pt;">AAAAABCD
THE_CAT_IN_THE_HAT
END

Sample Output

64 13 4.9
144 51 2.8


坑在只有一个字符的时候
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
#define maxn 1005
#define inf 0x3f3f3f3f
int main()
{
    char s[maxn];
    int a[maxn];
    freopen("in.txt","r",stdin);
    while(gets(s)){
        if(!strcmp(s,"END"))break;
        int m=0;
        int sum=0;
        m=strlen(s);
        priority_queue<int>q;
        memset(a,0,sizeof a);
        for(int i=0;i<m;i++){
            if(s[i]>='A'&&s[i]<='Z')
            a[s[i]-'A'+1]++;
            else a[0]++;
        }
        for(int i=0;i<=26;i++)
            if(a[i]>0)q.push(-a[i]);
        if(q.size()==1)sum+=-(q.top());
        else{
        while(q.size()>1){
            int a=-q.top();
            q.pop();
            int b=-q.top();
            q.pop();
            sum+=a+b;
            q.push(-(a+b));
        }
        }
        printf("%d %d %.1f\n",m*8,sum,1.0*m*8/sum);
    }
}

Entropy(哈弗曼编码,优先队列)

标签:entropy   哈弗曼编码   优先队列   

原文地址:http://blog.csdn.net/u013497977/article/details/44006989

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!