好贷网好贷款

Hdu1053 step5.2.8(哈夫曼树)

发布时间:2016-12-5 4:33:46 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"Hdu1053 step5.2.8(哈夫曼树)",主要涉及到Hdu1053 step5.2.8(哈夫曼树)方面的内容,对于Hdu1053 step5.2.8(哈夫曼树)感兴趣的同学可以参考一下。

Entropy   Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3548    Accepted Submission(s): 1393     Problem Description An entropy encoder is a data encodingmethod that achieves lossless 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 to accuratelyencode the message. A high degree of entropy implies a message with a greatdeal of wasted information; english text encoded in ASCII is an example of amessage type that has very high entropy. Already compressed messages, such asJPEG graphics or ZIP archives, have very little entropy and do not benefit fromfurther attempts at entropy encoding.   English text encoded in ASCII has a highdegree of entropy because all characters are encoded using the same number ofbits, eight. It is a known fact that the letters E, L, N, R, S and T occur at aconsiderably higher frequency than do most other letters in english text. If away could be found to encode just these letters with four bits, then the newencoding would be smaller, would contain all the original information, andwould 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 torepresent each possible glyph or character. How would an encoding scheme thatused four bits for the above letters be able to distinguish between thefour-bit codes and eight-bit codes? This seemingly difficult problem is solvedusing what is known as a “prefix-free variable-length” encoding.   In such an encoding, any number of bits canbe used to represent any glyph, and glyphs not present in the message aresimply not encoded. However, in order to be able to recover the information, nobit pattern that encodes a glyph is allowed to be the prefix of any otherencoding 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 glyphcan be decoded. If the prefix-free constraint was not enforced, then such adecoding would be impossible.   Consider the text “AAAAABCD”. Using ASCII,encoding this would require 64 bits. If, instead, we encode “A” with the bitpattern “00”, “B” with “01”, “C” with “10”, and “D” with “11” then we canencode this text in only 16 bits; the resulting bit pattern would be“0000000000011011”. This is still a fixed-length encoding, however; we’re usingtwo bits per glyph instead of eight. Since the glyph “A” occurs with greaterfrequency, 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 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 notthe only optimal encoding, as it is obvious that the encodings for B, C and Dcould be interchanged freely for any given encoding without increasing the sizeof the final encoded message.) Using this encoding, the message encodes in only13 bits to “0000010110111”, a compression ratio of 4.9 to 1 (that is, each bitin the final encoded message represents as much information as did 4.9 bits inthe original encoding). Read through this bit pattern from left to right andyou’ll see that the prefix-free encoding makes it simple to decode this intothe original text even though the codes have varying bit lengths.   As a second example, consider the text “THECAT IN THE HAT”. In this text, the letter “T” and the space character bothoccur with the highest frequency, so they will clearly have the shortestencoding bit patterns in an optimal encoding. The letters “C”, “I’ and “N” onlyoccur once, however, so they will have the longest codes.   There are many possible sets of prefix-freevariable-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 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 therefore requires only 51 bits compared tothe 144 that would be necessary to encode the message with 8-bit ASCIIencoding, a compression ratio of 2.8 to 1.     Input The input file will contain a list of textstrings, one per line. The text strings will consist only of uppercasealphanumeric 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, outputthe length in bits of the 8-bit ASCII encoding, the length in bits of anoptimal prefix-free variable-length encoding, and the compression ratioaccurate to one decimal point.     Sample Input AAAAABCD THE_CAT_IN_THE_HAT END     Sample Output 64 13 4.9 144 51 2.8 题解: 这道题是,哈弗曼树的解释。即求树的最短带权路径,求wpl。过程就是建立哈夫曼树,然后计算顶点到叶节点的路径长度,用长度*权,累加得到最短带权路径的值。 源代码: #include <iostream> #include <stdio.h> #include <string> #include <map>   using namespace std; int w[100]; typedef struct {    unsignedint weight;    unsignedint parent,lchild,rchild; }htnode,*huffmantree;   void select(huffmantree& ht,intpos,int& s1,int&s2) {//在¨²ht[1..pos]中D选?择?parent为a0且¨°weight最Á?小?的Ì?两¢?个?结¨¢点Ì?,ê? //序¨°号?为as1,ê?s2;ê?    huffmantree p = ht;    inti = 0,mx = 9999999;    for(;i< pos;i++,p++)    {      if(p->parent== 0)         if(mx> p->weight)         {           s1 = i;           mx = p->weight;         }    }      p = ht;       (p+s1)->parent = 1;      mx = 9999999;    for(p= ht,i = 0;i < pos;i++,p++)    {      if(p->parent== 0)         if(mx> p->weight)         {           s2 = i;           mx = p->weight;         }    } }   int huffmancoding(huffmantree &ht, int n,int *w) {    if(n<= 1)      returnw[1];    intm = 2*n-1;    ht = (huffmantree)malloc((m+1)*sizeof(htnode)); //ht就¨ª是º?要°a构1造¨¬的Ì?哈t夫¤¨°曼¨¹树º¡Â;ê?    huffmantree p;    inti;    w++;    for(p= ht,i = 1;i <= m;++i,++p,++w)    {      p->weight = *w;      p->lchild = 0;      p->rchild = 0;      p->parent = 0;    }      for(i= n;i < m;i++)    {//构1造¨¬哈t弗£¤曼¨¹树º¡Â      ints1,s2;      select(ht,i,s1,s2);      ht[s1].parent = i;      ht[s2].parent = i;      ht[i].lchild = s1;      ht[i].rchild = s2;      ht[i].weight =ht[s1].weight+ht[s2].weight;    }    intsum = 0;    for(int i = 0;i < n;i++)    {      intcount = 0;      for(int f = ht[i].parent; f != 0;count++,f =ht[f].parent);      sum += count* ht[i].weight;    }    returnsum; } int main() {    string str;    while(cin>>str && str != "END")    {      charch[100];      huffmantree ht;      intn = 1;        memset(w,0,sizeof(w));      ch[1] = str[0];      w[1] =1;      for(int i = 1;i < str.size();i++)      {          intflag = 1;         for(int k = 1; k <= n;k++)         {           if(ch[k]== str[i])           {              w[k]++;              flag = 0;              break;           }         }         if(flag)         {           n++;           ch[n] = str[i];           w[n] = 1;         }      }        intvalue = huffmancoding(ht,n,w);      intans1 = str.size()*8;      doubleb = ans1*1.0/(value*1.0);      printf("%d%d %.1lf\n",ans1,value,b);    }    return0; }  

上一篇:Win03安装打印机的问题 打印机驱动程序与您计算机上启用的阻止NT4.0驱动程序的策略不兼
下一篇:数据结构——链栈的实现(C语言)

相关文章

相关评论