数据结构实验之二叉树六:哈夫曼编码

Time Limit: 1000 ms Memory Limit: 65536 KiB

Problem Description

字符的编码方式有多种,除了大家熟悉的ASCII编码,哈夫曼编码(Huffman Coding)也是一种编码方式,它是可变字长编码。该方法完全依据字符出现概率来构造出平均长度最短的编码,称之为最优编码。哈夫曼编码常被用于数据文件压缩中,其压缩率通常在20%~90%之间。你的任务是对从键盘输入的一个字符串求出它的ASCII编码长度和哈夫曼编码长度的比值。

Input

 输入数据有多组,每组数据一行,表示要编码的字符串。

Output

 对应字符的ASCII编码长度la,huffman编码长度lh和la/lh的值(保留一位小数),数据之间以空格间隔。

Sample Input

AAAAABCD
THE_CAT_IN_THE_HAT

Sample Output

64 13 4.9
144 51 2.8
#include <stdio.h>
#include <stdlib.h>
#include<string.h> void arrange( int *a, int lt, int rt )
{
int key = a[lt], i = lt, j = rt;
if( i >= j ) return ;
while( i < j )
{
while( i < j && a[j] >= key )
j--;
a[i] = a[j];
while( i < j && a[i] <= key )
i++;
a[j] = a[i];
}
a[i] = key;
arrange( a, lt, i-1 );
arrange( a, i+1, rt);
} int main()
{
int len, i;
char s[500];
int q[1000],t[1000];
while(~scanf("%s",s))
{
int head = 0, tail = 0;
memset( t, 0, sizeof(t) );
len = strlen(s) ;
for( i = 0; i < len; i++ )
{
t[s[i]]++;
}
for( i = 0; i < 500; i++)
{
if( t[i] )
{
q[head++] = t[i];
}
}
arrange( q, 0, head-1 ); int sum = 0;
int a, b;
while( head != tail )
{
a = q[tail++];
if( head != tail )
{
b = q[tail++];
sum += a + b;
q[head++] = a + b;
arrange( q, tail, head-1 );
}
}
printf("%d %d %.1lf\n", len*8, sum, 1.0*len*8/sum );
}
return 0;
}

SDUT OJ 数据结构实验之二叉树六:哈夫曼编码的更多相关文章

  1. SDUT 3345 数据结构实验之二叉树六:哈夫曼编码

    数据结构实验之二叉树六:哈夫曼编码 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 字符的编 ...

  2. SDUT OJ 数据结构实验之图论六:村村通公路(最小生成树)

    数据结构实验之图论六:村村通公路 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descri ...

  3. SDUT OJ 数据结构实验之二叉树八:(中序后序)求二叉树的深度

    数据结构实验之二叉树八:(中序后序)求二叉树的深度 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Probl ...

  4. SDUT OJ 数据结构实验之二叉树七:叶子问题

    数据结构实验之二叉树七:叶子问题 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descri ...

  5. SDUT OJ 数据结构实验之二叉树五:层序遍历

    数据结构实验之二叉树五:层序遍历 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descri ...

  6. SDUT OJ 数据结构实验之二叉树四:(先序中序)还原二叉树

    数据结构实验之二叉树四:(先序中序)还原二叉树 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem ...

  7. SDUT OJ 数据结构实验之二叉树三:统计叶子数

    数据结构实验之二叉树三:统计叶子数 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descr ...

  8. SDUT OJ 数据结构实验之二叉树二:遍历二叉树

    数据结构实验之二叉树二:遍历二叉树 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descr ...

  9. SDUT OJ 数据结构实验之二叉树一:树的同构

    数据结构实验之二叉树一:树的同构 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descri ...

随机推荐

  1. sendMail在centos下的安装

    一.sendEmail介绍   SendEmail is a lightweight, command line SMTP email client. If you have the need to ...

  2. 异常:java.lang.IllegalStateException: Ambiguous handler methods mapped for HTTP path '/app/userInfoMaint/getProvince.do'

    调试代码时出现异常:java.lang.IllegalStateException: Ambiguous handler methods mapped for HTTP path '/app/user ...

  3. 常用Linux命令-文件上传和下载

    rz 上传本地文件到远程服务器 sz fileName 下载文件到本地电脑 如果不能使用以上命令进行文件上传和下载需要安装命令,步骤如下: 1.软件安装1)编译安装root 账号登陆后,依次执行以下命 ...

  4. 6410在rvds下编译启动代码报错分析

    contains invalid call from '~PRES8' function to 'REQ8' function main RVDS编译出现contains invalid call f ...

  5. day63-webservice 02.cxf环境搭建

    指定bin目录的目的是在docs窗口可以直接来执行这里面的命令. ANT这里面

  6. c++多线程编程(一)

    C++本身并没有提供任何多线程机制,但是在windows下,我们可以调用SDK win32 api来编写多线程的程序,下面就此简单的讲一下: 创建线程的函数 HANDLE CreateThread( ...

  7. 【bzoj1016】[JSOI2008]最小生成树计数

    1016: [JSOI2008]最小生成树计数 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 4863  Solved: 1973[Submit][St ...

  8. 258. Add Digits 数位相加到只剩一位数

    [抄题]: Given a non-negative integer num, repeatedly add all its digits until the result has only one ...

  9. 1.介绍templates

    我们现在要计算int和double类型数据的平方,我们就需要2个函数: #include <iostream> using namespace std; int square(int x) ...

  10. LibreOJ 6280 数列分块入门 4(分块区间加区间求和)

    题解:分块的区间求和比起线段树来说实在是太好写了(当然,复杂度也高)但这也是没办法的事情嘛.总之50000的数据跑了75ms左右还是挺优越的. 比起单点询问来说,区间询问和也没有复杂多少,多开一个su ...