POJ - 1521 Entropy(哈夫曼編碼)

題目鏈接https://vjudge.net/contest/361484#problem/J
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

翻譯
給定一個字符串,只由大寫字母和下劃線組成。一位字符由8位ASCII編碼,也可以通過哈夫曼編碼編碼
輸出8位ASCII編碼的位長度 哈夫曼編碼的位長度 壓縮比

哈夫曼編碼:
最優前綴編碼。即對於n個字符,分別以它們的使用頻度作爲葉子權值來構造哈夫曼樹。
哈夫曼樹
由n個帶權葉子結點構成的所有二叉樹中帶權路徑長度最短的二叉樹。
樹的帶權路徑長度
從根到所有葉子結點的各個帶權路徑長度之和。
帶權路徑長度
從樹根到某一結點的路徑長度與該結點的權的乘積。
路徑長度
根結點到該結點所經過的分支數目

用靜態三叉鏈表存儲哈夫曼樹

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define N 60
#define M 2*N-1
int len;
typedef struct
{
    int weight;/*結點的權值*/
    int parent;/*雙親的下標*/
    int LChild;/*左孩子結點的下標*/
    int RChild;/*右孩子結點的下標*/
} HTNode,*HuffmanTree;
void select(HuffmanTree *ht,int n,int *s1,int *s2)
{
    int mi;
    for(int i=1; i<=n; i++)
    {
        if((*ht)[i].parent==0)/*找到其中任意一個可能的結果,沒有用過的結點parent=0*/
        {
            mi=i;
            i=n+1;
        }
    }
    for(int i=1; i<=n; i++)/*找到其中的一個最小值*/
    {
        if((*ht)[i].parent==0)
        {
            if((*ht)[i].weight<(*ht)[mi].weight)
                mi=i;
        }
    }
    *s1=mi;
    for(int i=1; i<=n; i++)
    {
        if((*ht)[i].parent==0&&i!=(*s1))
        {
            mi=i;
            i=n+1;
        }
    }
    for(int i=1; i<=n; i++)
    {
        if((*ht)[i].parent==0&&i!=(*s1))
        {
            if((*ht)[i].weight<(*ht)[mi].weight)
                mi=i;
        }
    }
    *s2=mi;
}
void CrtHuffmanTree(HuffmanTree *ht,int *w,int n)
{
    *ht=(HuffmanTree)malloc((M+1)*sizeof(HTNode));  /*0號單元未使用*/
    int s1,s2;
    for(int i=1; i<=n; i++)
    {
        (*ht)[i].weight = w[i-1];
        (*ht)[i].LChild = 0;
        (*ht)[i].parent = 0;
        (*ht)[i].RChild = 0;
    }
    int m=2*n-1;/*n個結點的哈夫曼樹共有2*n-1個結點*/
    for(int i=n+1; i<=m; i++)
    {
        (*ht)[i].weight=0;
        (*ht)[i].LChild=0;
        (*ht)[i].parent=0;
        (*ht)[i].RChild=0;
    }
    for(int i=n+1; i<=m; i++)  /*創建非葉子結點,建哈夫曼樹*/
    {
        /*(*ht)[1]~(*ht)[i-1]的範圍內選擇兩個parent爲0且weight最小的結點,其序號分別賦值給s1、s2返回*/
        select(ht,i-1,&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;
    }
}
void print(HuffmanTree *ht,int n)
{
    int sum=0,cnt,k;
    for(int i=1; i<=n; i++)/*n個葉子結點*/
    {
        k=i;
        cnt=0;
        while((*ht)[k].parent!=0)
        {
            k=(*ht)[k].parent;
            cnt++;/*分支數目*/
        }
        sum+=cnt*(*ht)[i].weight;
    }
    float rat=(len*8.0)/sum;
    printf("%d %d %.1f\n",len*8,sum,rat);
}
int main()
{
    HuffmanTree HT;
    char ch[100000];
    int book[N];
    int j;
    while(gets(ch))
    {
        if(strcmp(ch,"END")==0)
            break;
        int *w;
        memset(book,0,sizeof(book));
        w=(int *)malloc((N+1)*sizeof(int));/*開闢一個w數組*/
        int t=0;
        len=strlen(ch);
        for(int i=0; ch[i]!='\0'; i++)
        {
            if(ch[i]=='_')
                t++;
            else
                book[ch[i]-'A']++;/*每個字母出現的頻度*/
        }
        book[26]=t;/*0~2526個英文字母*/
        int j=0;/*j個葉子權值*/
        for(int i=0; i<26; i++)
        {
            if(book[i])
                w[j++]=book[i];
        }
        if(t)/*下劃線存在*/
            w[j++]=t;/*j是葉子結點的個數*/
        if(j==1)
            printf("%d %d %.1f\n",len*8,w[0],len*8*1.0/w[0]);
        else
        {
            CrtHuffmanTree(&HT,w,j);
            print(&HT,j);
        }
    }
    return 0;
}

簡化指針

#include<cstdio>
#include<cstring>
#include<cstdlib>
typedef struct Node
{
    int weight;
    int parent,left,right;
} HTNode,*HuffmanTree;
typedef char *HuffmanCode;
int len;
void HuffmanCoding(int *w,int n)
{
    int *q=w;
    HuffmanTree HT;
    HuffmanTree p;
    int m,i,j,s1,s2,min1,min2,c,f,sum=0;
    if(n<1)
        return;
    m=2*n-1;
    HT=new HTNode[m+1];           //0號單元沒用
    for(p=HT+1,i=1; i<=n; ++i,++p,++w)    //初始化
    {
        p->weight=*w;
        p->parent=0;
        p->left=0;
        p->right=0;
    }
    for(; i<=m; ++i,++p)
    {
        p->weight=0;
        p->parent=0;
        p->left=0;
        p->right=0;
    }
    for(i=n+1; i<=m; ++i)
    {
        min1=min2=0x7fffffff;
        //在HT[1,2.....i-1]中選擇parent爲0且weight值最小的兩個結點,其序號分別爲s1、s2
        for(j=1; j<i; ++j)
        {
            if(HT[j].parent==0&&min1>HT[j].weight)
            {
                min1=HT[j].weight;
                s1=j;
            }
        }
        HT[s1].parent=i;
        for(j=1; j<i; ++j)
        {
            if(HT[j].parent==0&&min2>HT[j].weight)
            {
                min2=HT[j].weight;
                s2=j;
            }
        }
        HT[s2].parent=i;
        HT[i].left=s1;
        HT[i].right=s2;
        HT[i].weight=HT[s1].weight+HT[s2].weight;
    }
    //從葉子節點到根逆向求
    for(i=1; i<=n; ++i)        //逐個字符求其編碼長度
    {
        int temp=0;
        for(c=i,f=HT[i].parent; f; c=f,f=HT[f].parent) //從葉子到根逆向求編碼
            ++temp;
        sum=sum+temp*q[i-1];          //編碼長度要乘以權值(即該字符的個數)纔是總長度
    }
    int bit=len*8;
    printf("%d %d %.1f\n",len*8,sum,bit*1.0/sum);
}
int main()
{
    char s[1000];
    int i,j,t;
    int weight[30];
    while(gets(s))
    {
        t=0;
        if(strcmp(s,"END")==0)
            break;
        memset(weight,0,sizeof(weight));
        len=strlen(s);
        for(i=0; i<len; ++i)
        {
            if(s[i]=='_')
                ++t;
            else
                ++weight[s[i]-'A'];
        }
        weight[26]=t;
        for(i=0,j=0; i<26; ++i)  //去掉沒有出現過的字母,將權值都壓縮存儲到原來的空間中
            if(weight[i])
                weight[j++]=weight[i];
        if(t)      //如果有下劃線的話
            weight[j++]=t;
        if(j==1)     //就一種字符出現的話直接輸出---因爲赫夫曼編碼要找到最小的兩個合併,如果只有一個的話會出錯
            printf("%d %d %.1f\n",len*8,weight[0],len*8*1.0/weight[0]);
        else
            HuffmanCoding(weight,j);
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章