求英文文章的Huffman编码长度 很急!!!在线等!!!
一、题目
英文文章的Huffman编码。
二、题目描述
我们知道,字符在计算机中是以它的ASCII码表达的,而普通字符的ASCII都是7个
二进制位,实际上就是用一个字节(8个二进制位)存储的。如果一篇英文文章有100个
字符,则其长度为800个二进制位。通过网络传输一篇英文文章时如果能对它先压缩,
然后在接收方进行解压,就可以提高网络传输速度。Huffman编码就是一种方便在发送
发进行压缩和接收方解压的编码方式。
现要求从键盘输入一篇英文文章(保证只含小写英文字母和空格,没有任何其他
字符,但并不保证所有小写字母都同时出现),使用Huffman编码方式对该文章中的所
有字符重新编码,以实现对它的压缩,然后输出压缩比(即压缩后的二进制位数除以
压缩前的二进制位数)
输入
从键盘上输入一片英文文章
输出
输出压缩比。四舍五入后保留小数点后两位。例如结果为0.325,则输出0.33,下面的输出都算错误:
0.32; .33; 0.325; 00.33; +0.33; +.33
样例输入
this is just a test
样例输出
0.37
求大神秒了!!!在线等!!!
[解决办法]
代码已经贴在你的另一个帖子了。
这里重复一遍。
#include <stdio.h>#include <stdlib.h>#include <memory.h>#include <string.h>/* 该程序用来对字符串进行huffman编码 编码用的huffman数节点的数据结构1. weight: UINT32, 权重值2. parent: UINT16, 0xffff表示空指针3. l_child: UINT16: 0xffff表示空指针4. r_child: UINT16: 0xffff表示空指针*/typedef unsigned long UINT32;typedef unsigned short UINT16;typedef unsigned char BYTE;typedef struct{ UINT32 weight; UINT16 parent; UINT16 l_child; UINT16 r_child;}HM_ENCODE_NODE;#define NIL_PT 0xffff#define SLICE_ARRAY_SIZE(slice_arr) (slice_arr.tail-slice_arr.head+1)HM_ENCODE_NODE g_HTree[256*2]; //global array/* 快速排序比较函数 */int cmp_by_weight(const void *a ,const void *b){ HM_ENCODE_NODE** pp1=(HM_ENCODE_NODE **)a; HM_ENCODE_NODE** pp2=(HM_ENCODE_NODE **)b; HM_ENCODE_NODE* p1=*pp1; HM_ENCODE_NODE* p2=*pp2; if ( p1->weight < p2->weight) return -1; else if ( p1->weight > p2->weight) return 1; else if ( p1<p2) return -1; else if (p1>p2) return 1; else return 0;}/* 根据buff的内容,建立huffman树g_HTree */void createHuffmanTree(char *buff,int len){ //a fix space (256 pointer) size slice array //The array space is fix, the element count = tail-head+1 typedef struct { HM_ENCODE_NODE* arr[256]; int head; int tail; }SLICE_ARR; SLICE_ARR HT_Arr; int new_node_idx,i,j,i1,i2; memset(g_HTree,0,sizeof(g_HTree)); for (i=0;i<256;i++) g_HTree[i].parent=NIL_PT; for (i=0;i<len;i++) { char ch=buff[i]; g_HTree[ch].weight++; } for (j=0,i=0;i<256;i++) { if ( g_HTree[i].weight>0) HT_Arr.arr[j++]= &(g_HTree[i]); } HT_Arr.head=0; HT_Arr.tail=j-1; qsort( HT_Arr.arr,SLICE_ARRAY_SIZE(HT_Arr),sizeof(HM_ENCODE_NODE*),cmp_by_weight); //对数组按照weight排序 j=257; //the position 256 is reserved for root pointer while ( SLICE_ARRAY_SIZE(HT_Arr)>=2) { if (HT_Arr.tail==255) { memmove(HT_Arr.arr,HT_Arr.arr+HT_Arr.head,sizeof(HM_ENCODE_NODE*)*SLICE_ARRAY_SIZE(HT_Arr)); HT_Arr.tail-=HT_Arr.head; HT_Arr.head=0; } i1=HT_Arr.head; i2=HT_Arr.head+1; if (SLICE_ARRAY_SIZE(HT_Arr)==2) //the new node is root node new_node_idx=256; //keep the root node is the first place else new_node_idx=j++; g_HTree[new_node_idx].weight= HT_Arr.arr[i1]->weight +HT_Arr.arr[i2]->weight; g_HTree[new_node_idx].l_child= HT_Arr.arr[i1] - g_HTree; g_HTree[new_node_idx].r_child= HT_Arr.arr[i2] - g_HTree; g_HTree[new_node_idx].parent=NIL_PT; HT_Arr.arr[i1]->parent=HT_Arr.arr[i2]->parent=new_node_idx; HT_Arr.head+=2; //remove 2 element from header from HT_Arr //insert the now element to proper position i=HT_Arr.tail; while (i>=HT_Arr.head && HT_Arr.arr[i]->weight > g_HTree[new_node_idx].weight) { HT_Arr.arr[i+1] =HT_Arr.arr[i] ; i--; } HT_Arr.arr[i+1] = &(g_HTree[new_node_idx]); //这个位置插入新元素 HT_Arr.tail++; }}int getHuffman_code_bits_count(char *szString){ UINT32 i,j; int total_bits_count; total_bits_count=0; for (i=0;i<strlen(szString);i++) { j=szString[i]; while ( g_HTree[j].parent !=NIL_PT) { j=g_HTree[j].parent; total_bits_count++; } } return total_bits_count;}int main(int argc, char* argv[]){ char *szString; char buff[256]; int bits_count; double f; //strcpy(buff,"this is a test"); gets(buff); createHuffmanTree(szString,strlen(szString)); bits_count=getHuffman_code_bits_count(szString); f=(double)bits_count /(double)(strlen(szString)*8); printf("%.2f\n",f); return 0;}