一、题目描述

In computer science and information theory, a Huffman code is an optimal prefix code algorithm.

In this exercise, please use Huffman coding to encode a given data.

You should output the number of bits, denoted as B(T), to encode the data:

B(T)=∑f(c)dT(c),

where f(c) is the frequency of character c, and dT(c) is be the depth of character c’s leaf in the tree T.

二、输入

The first line is the number of characters n.

The following n lines, each line gives the character c and its frequency f(c).

三、输出

Output a single number B(T).

例如:

输入:

5

0 5

1 4

2 6

3 2

4 3

输出:

45

四、解题思路

1、所有的节点存放在一个数组中

2、对数组按每个节点的权值从小到大进行排序

本文使用快速排序

3、取出权值最小的两个节点,生成新的节点,原来的两个节点分别为新的节点的左右子树,新节点的权值为原来两个节点的权值之和

4、把新的节点按照权值插入到已经有序的数组中

5、重复步骤3、4,直到只剩根节点

五、代码

#include<iostream>
#include<string>
#include <vector> using namespace std; //节点结构
struct Node{
char ch;
int weight;
Node *leftChild, *rightChild;
}; //插入排序
void insertSort(Node** nodeArray, int low, int high)
{
for(int i = low + 1; i < high; i++)
{
Node* temp = nodeArray[i];
int j = i - 1;
while(j >= low && nodeArray[j]->weight > temp->weight)
{
nodeArray[j+1] = nodeArray[j];
j--;
}
nodeArray[j+1] = temp;
}
} //建Hffman树
void createHuffman(Node** nodeArray, int n)
{ insertSort(nodeArray, 0, n); //先对节点按weight排序
int p = 0;
int res = 0;
while(p < n - 1)
{
Node* minNode1 = nodeArray[p]; //找出weight值最小的两个
Node* minNode2 = nodeArray[++p];
Node* newNode = new Node;
newNode->weight = minNode1->weight + minNode2->weight; //合并生成新的节点
res += newNode->weight; newNode->leftChild = minNode1;
newNode->rightChild = minNode2;
nodeArray[p] = newNode;
insertSort(nodeArray, p, n);
} cout << res <<endl;
} int main()
{
int n;
cin >> n;
char ch;
int weight;
Node* nodeArray[n];
for(int i = 0; i < n; i++)
{
Node* node = new Node;
cin >> ch >> weight;
node->ch = ch;
node->weight = weight;
nodeArray[i] = node;
}
createHuffman(nodeArray, n);
return 0;
}

<Sicily>Huffman coding的更多相关文章

  1. hdu 1053 (huffman coding, greedy algorithm, std::partition, std::priority_queue ) 分类: hdoj 2015-06-18 19:11 22人阅读 评论(0) 收藏

    huffman coding, greedy algorithm. std::priority_queue, std::partition, when i use the three commente ...

  2. [IR] Huffman Coding

    为了保证:Block中,所有的叶子在所有的中间结点的前面.Static: Huffman coding Dynamic: Adaptive Huffman 一些概念 压缩指标 • Compress a ...

  3. 霍夫曼编码(Huffman Coding)

    霍夫曼编码(Huffman Coding)是一种编码方法,霍夫曼编码是可变字长编码(VLC)的一种. 霍夫曼编码使用变长编码表对源符号(如文件中的一个字母)进行编码,其中变长编码表是通过一种评估来源符 ...

  4. 哈夫曼编码(Huffman coding)的那些事,(编码技术介绍和程序实现)

    前言 哈夫曼编码(Huffman coding)是一种可变长的前缀码.哈夫曼编码使用的算法是David A. Huffman还是在MIT的学生时提出的,并且在1952年发表了名为<A Metho ...

  5. 【CodeForces】700 D. Huffman Coding on Segment 哈夫曼树+莫队+分块

    [题目]D. Huffman Coding on Segment [题意]给定n个数字,m次询问区间[l,r]的数字的哈夫曼编码总长.1<=n,m,ai<=10^5. [算法]哈夫曼树+莫 ...

  6. 哈夫曼编码的理解(Huffman Coding)

    哈夫曼编码(Huffman Coding),又称霍夫曼编码,是一种编码方式,可变字长编码(VLC)的一种.Huffman于1952年提出一种编码方法,该方法完全依据字符出现概率来构造异字头的平均长度最 ...

  7. Huffman coding & Huffman tree

    Huffman coding & Huffman tree Huffman coding 哈夫曼编码 / 最优二元前缀码 Huffman tree 哈夫曼树 / 最优二叉树 https://w ...

  8. jpeg huffman coding table

    亮度DC系数的取值范围及序号:                                                               序号(size) 取值范围 0 0  1 - ...

  9. Huffman Coding

    哈夫曼树 霍夫曼编码是一种无前缀编码.解码时不会混淆.其主要应用在数据压缩,加密解密等场合. 1. 由给定结点构造哈夫曼树 (1)先从小到大排序(nlogn) (2)先用最小的两个点构造一个节点,父节 ...

随机推荐

  1. android sudio 执行的中文是乱码解决方式

    1.File-->Setings-->查找file encodings 例如以下图 2.将 IDE Encoding .Project Encoding.Default encoding ...

  2. Windows环境下通过Git来管理自己的Android代码

    前面已经介绍了在Windows下使用git工具来下载Android的源代码,Windows环境下通过Git得到Android源代码,这里记录我使用git工具来管理我自己的代码,git是一种分布式的项目 ...

  3. 集群版本升级——rolling upgrade在ES 单节点从 restart 到加入集群,大概要 100s 左右的时间。也就是说,这 100s 内,该节点上的所有分片都是 unassigned 状态

    集群版本升级 Elasticsearch 作为一个新兴项目,版本更新非常快.而且每次版本更新都或多或少带有一些重要的性能优化.稳定性提升等特性.可以说,ES 集群的版本升级,是目前 ES 运维必然要做 ...

  4. zzulioj--1712--神秘的数列(水题)

    1712: 神密的数列 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 122  Solved: 92 SubmitStatusWeb Board De ...

  5. js中cookie的使用 以及缺点

      什么是Cookie Cookie意为“甜饼”,是由W3C组织提出,最早由Netscape社区发展的一种机制.目前Cookie已经成为标准,所有的主流浏览器如IE.Netscape.Firefox. ...

  6. Windows下安装和使用MongoDB

    支持平台:从2.2版本开始,MongoDB不再支持Windows XP.要使用新版本的MongoDB,请用更新版本的Windows系统. 重要:如果你正在使用Windows Server 2008 R ...

  7. ubuntu上配tensorflow

    前一阵绕了大弯路,基本弄好了UEFI双系统后,面对的就是CUDA咋装在Linux. 好在教程好多,有些朋友建议先装CUDA再装显卡驱动.弄好之后记录一下详细过程吧. *** 这两天看了一些教程,还是感 ...

  8. 哪位大兄弟有用 cMake 开发Android ndk的

    一直用 Android studio 开发ndk,但是gradle支持的不是很好,只有experimental 版本支持 配置各种蛋疼.主要每次新建一个module都要修改配置半天.之前也看到过goo ...

  9. QT笔记 -- (3) 为QLabel添加鼠标响应方法1

    参考 http://qt-project.org/wiki/Make-a-QLabel-Clickable 1.首先重载QLabel的mousePressEvent,这样点击QLabel时就能发出cl ...

  10. Ubuntu系统下Import cv2提示no modules ...错误

    最近利用pycharm在Ubuntu系统下调试一个Python项目,将pycharm的解释器从python2.7更换到python3.4后,程序中的Import cv2提示no modules nam ...