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

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. leetcode216

    public class Solution { public IList<IList<int>> CombinationSum3(int k, int n) { , , , , ...

  2. 【原创】1. MYSQL++简介

    MYSQL++是对于MYSQL C API的C++完全包装. MYSQL++能够至少做如下几件事情 1. 连接数据库 通过TCP连接数据库 通过WINDOWS命名管道连接数据库 UNIX域SOCKET ...

  3. Composert 的命令

    (1) php artisan       ----查看所有的命令帮助 (2) php artisan make:controller StudentController      ----创建一个控 ...

  4. 686. Repeated String Match 字符串重复后的子字符串查找

    [抄题]: Given two strings A and B, find the minimum number of times A has to be repeated such that B i ...

  5. 屌爆的xamarin,一人单挑google/apple/windows

    一个IDE就把3大手机平台全包了: android:自带模拟器xamarin player,速度堪比genymotion. ios:需要一台mac机辅助,一旦配好后可全程脱离,连ios模拟器都给镜像到 ...

  6. MFC可视化 列表控件的使用

    1.应该加入头文件   #include <Atlbase.h>   2.示例     类向导给列表控件绑定变量m_list DWORD   dwExStyle=LVS_EX_FULLRO ...

  7. Laravel 测试教程

    参考链接:https://laravel-news.com/seeding-data-testing 迁移文件 修改 database/migrations/2014_10_12_000000_cre ...

  8. javascript总结16:数组array12

    1 Array 对象 作用:Array 对象用于在变量中存储多个值. 1.1 数组定义 var ary = new Array();//通过创建对象的方式创建数组 var ary1 = [];// 直 ...

  9. javascript总结4:javascript常见变量

    1 javascript变量 定义:变量在计算机中,是用于存储信息的"容器". 2  使用方法: 2-1 定义变量: var n1; 2-2 变量赋值: var n2=23.45; ...

  10. 第16章-使用Spring MVC创建REST API

    1 了解REST 1.1 REST的基础知识 REST与RPC几乎没有任何关系.RPC是面向服务的,并关注于行为和动作:而REST是面向资源的,强调描述应用程序的事物和名词. 为了理解REST是什么, ...