数据结构实验之二叉树六:哈夫曼编码
Time Limit: 1000MS
Memory Limit: 65536KB
Problem Description
字符的编码方式有多种,除了大家熟悉的ASCII编码,哈夫曼编码(Huffman Coding)也是一种编码方式,它是可变字长编码。该方法完全依据字符出现概率来构造出平均长度最短的编码,称之为最优编码。哈夫曼编码常被用于数据文件压缩中,其压缩率通常在20%~90%之间。你的任务是对从键盘输入的一个字符串求出它的ASCII编码长度和哈夫曼编码长度的比值。
Input
输入数据有多组,每组数据一行,表示要编码的字符串。
Output
对应字符的
ASCII
编码长度
la
,
huffman
编码长度
lh
和
la/lh
的值
(
保留一位小数
)
,数据之间以空格间隔。
Example Input
AAAAABCD THE_CAT_IN_THE_HAT
Example Output
64 13 4.9 144 51 2.8#include <bits/stdc++.h> #include <cstring> #include <algorithm> using namespace std; bool cmp(int a,int b) { return a<b; } void qsort(int l,int r, int a[]) { int i=l,j=r; int x=a[i]; if(l>=r) return ; while(i<j) { while(i<j&&a[j]>=x) j--; a[i]=a[j]; while(i<j&&a[i]<=x) i++; a[j]=a[i]; } a[i]=x; qsort(l,i-1,a); qsort(i+1,r,a); } int main() { int i,j; char s[3124]; int t[610],q[3124]; while(scanf("%s",s)!=EOF) { int top=0,rear=0; memset(t,0,sizeof(t)); memset(q,0,sizeof(q)); int len = strlen(s); int sum1 = 8*len,sum2=0; //ASCII一个字符占八个字节 for(i=0; i<len; i++) t[s[i]]++; //以ASCII码为下标 for(i=0; i<256; i++) //ASCII码最大为255 0到255 { if(t[i]!=0) q[top++]=t[i]; } // sort(q,q+top,cmp); qsort(0,top-1,q); while(top != rear) { int x1 = q[rear++];//每次取前两个最小的向前 ,然后放入队列中重新排 if(top != rear) { int x2=q[rear++]; sum2 += x1 + x2; q[top++]= x1+x2; // sort(q,q+top,cmp); qsort(rear,top-1,q); } } printf("%d %d %.1lf\n",sum1,sum2,sum1*1.0/sum2); } return 0; }