SDAU课程练习1003

Problem Description

An entropy encoder is a data encoding method that achieveslossless data compression by encoding a message with "wasted" or"extra" information removed. In other words, entropy encodingremoves information that was not necessary in the first place toaccurately encode the message. A high degree of entropy implies amessage with a great deal of wasted information; english textencoded in ASCII is an example of a message type that has very highentropy. Already compressed messages, such as JPEG graphics or ZIParchives, have very little entropy and do not benefit from furtherattempts at entropy encoding.

English text encoded in ASCII has a high degree of entropy becauseall characters are encoded using the same number of bits, eight. Itis a known fact that the letters E, L, N, R, S and T occur at aconsiderably higher frequency than do most other letters in englishtext. If a way could be found to encode just these letters withfour bits, then the new encoding would be smaller, would containall the original information, and would have less entropy. ASCIIuses a fixed number of bits for a reason, however: it’s easy, sinceone is always dealing with a fixed number of bits to represent eachpossible glyph or character. How would an encoding scheme that usedfour bits for the above letters be able to distinguish between thefour-bit codes and eight-bit codes? This seemingly difficultproblem is solved using what is known as a "prefix-freevariable-length" encoding.

In such an encoding, any number of bits can be used to representany glyph, and glyphs not present in the message are simply notencoded. However, in order to be able to recover the information,no bit pattern that encodes a glyph is allowed to be the prefix ofany other encoding bit pattern. This allows the encoded bitstreamto be read bit by bit, and whenever a set of bits is encounteredthat represents a glyph, that glyph can be decoded. If theprefix-free constraint was not enforced, then such a decoding wouldbe impossible.

Consider the text "AAAAABCD". Using ASCII, encoding this wouldrequire 64 bits. If, instead, we encode "A" with the bit pattern"00", "B" with "01", "C" with "10", and "D" with "11" then we canencode this text in only 16 bits; the resulting bit pattern wouldbe "0000000000011011". This is still a fixed-length encoding,however; we’re using two bits per glyph instead of eight. Since theglyph "A" occurs with greater frequency, could we do better byencoding it with fewer bits? In fact we can, but in order tomaintain a prefix-free encoding, some of the other bit patternswill 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 obviousthat the encodings for B, C and D could be interchanged freely forany given encoding without increasing the size of the final encodedmessage.) Using this encoding, the message encodes in only 13 bitsto "0000010110111", a compression ratio of 4.9 to 1 (that is, eachbit in the final encoded message represents as much information asdid 4.9 bits in the original encoding). Read through this bitpattern from left to right and you’ll see that the prefix-freeencoding makes it simple to decode this into the original text eventhough the codes have varying bit lengths.

As a second example, consider the text "THE CAT IN THE HAT". Inthis text, the letter "T" and the space character both occur withthe highest frequency, so they will clearly have the shortestencoding bit patterns in an optimal encoding. The letters "C", "I’and "N" only occur once, however, so they will have the longestcodes.

There are many possible sets of prefix-free variable-length bitpatterns that would yield the optimal encoding, that is, that wouldallow the text to be encoded in the fewest number of bits. One suchoptimal 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 thereforerequires only 51 bits compared to the 144 that would be necessaryto encode the message with 8-bit ASCII encoding, a compressionratio of 2.8 to 1.
 

Input
The input file will contain a list of text strings, one perline. The text strings will consist only of uppercase alphanumericcharacters and underscores (which are used in place of spaces). Theend of the input will be signalled by a line containing only theword “END” as the text string. This line should not beprocessed.
 

Output
For each text string in the input, output the length in bitsof the 8-bit ASCII encoding, the length in bits of an optimalprefix-free variable-length encoding, and the compression ratioaccurate to one decimal point.
 

Sample Input
AAAAABCD
THE_CAT_IN_THE_HAT
END
 

Sample Output
64 134.9
144 512.8

题目大意:

这个题目读起来还是比较麻烦的。哈弗曼树。也就是文件压缩技术。比如 AAAAABCD 就压缩成 1 01 010011  (写成 0 10 110 111 都一样) 然后计算压缩后的长度。

思路:

一种方法是模拟建树比较麻烦,一种直接模拟编码过程就行啦。其实理解了编码过程,直接求就行了。主要还是对树的理解,从大到小排序之后。两两建树,左0 右 1 。然后计算长度就行啦。

感想:

一开始没考虑到 队列清 0 ,wa 了,后来看到了,就 过了。

AC代码:

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
struct myComp
{
    booloperator () (const int &a,const int &b)
    {
       return a>b;
    }
};
int main()
{
   //freopen("r.txt", "r", stdin);
    strings;
    int i;
    intdd[60];
   priority_queue,myComp> pq;
   while(cin>>s)
    {
       if(s=="END") break;
       int num=0;
       if(s.length()==1)
       {
           cout<<8<<" "<<1<<""<<8<<endl;
           continue;
       }
       memset(dd,0,sizeof(dd));

              for(i=0;i
              {
                      if(s[i]=='_') dd[26]++;
                      else dd[s[i]-'A']++;
              }

              for(i=0;i<27;i++)
                      if(dd[i]!=0) pq.push(dd[i]);
              int t;

              if(pq.size()==1)
              {
                      for(i=0;i<27;i++)
                              if(dd[i]!=0)
                                      t=dd[i];
                      cout<<t*8<<" "<<t<<" 8.0";
                      cout<<endl;
                      pq.pop();
                      continue;
              }

              int sum=0;
              int a,b;
              while(pq.size()>1)
              {
                      a=pq.top();
                      pq.pop();
                      b=pq.top();
                      pq.pop();
                      sum+=a+b;
                      pq.push(a+b);
              }

              pq.pop();
              cout<<s.length()*8<<" "<<sum<<" ";
              printf("%.1f",s.length()*8.0/sum);
              cout<<endl;


      }

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值