hdoj 1053 Entropy(用哈夫曼编码)优先队列
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1053
讲解:
题意:给定一个字符串,根据哈夫曼编码求出最短长度,并求出比值。
思路:就是哈夫曼编码。把单个字符出现次数作为权值。
AC代码:
#include <iostream>
#include <string>
#include <queue>
#include <cstdio>
using namespace std; class node{
public:
int key; //a,b,c...
int count;//频率
int p;//父亲结点
friend bool operator < (const node &a, const node &b)
{
if(b.count < a.count)
return true;
else
return false;
}
}; int value(char c)
{
if(c=='_')
return ;
else
return(c-'A');
}
int main()
{
string str;
cin >> str;
while(str!="END")
{
node c[];
for(int i=;i<;i++)
{
c[i].key=i;
c[i].count=;
}
int length=str.length();
priority_queue<node> q;
for(int i=;i<length;i++)
{
(c[value(str.at(i))]).count++;
}
for(int i=;i<=;i++)
{
if(c[i].count!=)
q.push(c[i]);
}
if(q.size()==)
{
printf("%d %d 8.0\n",*length,length);
}
else
{
int high=;
int n=;
while(q.size()>)
{
node s1=q.top();
q.pop();
node s2=q.top();
q.pop();
c[n].count=s1.count+s2.count;
c[s1.key].p=n;
c[s2.key].p=n;
high=high+c[n].count;
q.push(c[n]);
n++;
}
printf("%d %d %.1lf\n",*length,high,(((double)(*length))/((double)high)));
}
cin >> str;
}
}
hdoj 1053 Entropy(用哈夫曼编码)优先队列的更多相关文章
- HDU 1053 Entropy(哈夫曼编码 贪心+优先队列)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1053 Entropy Time Limit: 2000/1000 MS (Java/Others) ...
- HDU 1053 & HDU 2527 哈夫曼编码
http://acm.hdu.edu.cn/showproblem.php?pid=1053 #include <iostream> #include <cstdio> #in ...
- *HDU1053 哈夫曼编码
Entropy Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- 图像压缩编解码实验(DCT编码+量化+熵编码(哈夫曼编码))【MATLAB】
课程要求 Assignment IV Transform + Quantization + Entropy Coding Input: an intra-frame or a residue pict ...
- 哈夫曼(huffman)树和哈夫曼编码
哈夫曼树 哈夫曼树也叫最优二叉树(哈夫曼树) 问题:什么是哈夫曼树? 例:将学生的百分制成绩转换为五分制成绩:≥90 分: A,80-89分: B,70-79分: C,60-69分: D,<60 ...
- (转载)哈夫曼编码(Huffman)
转载自:click here 1.哈夫曼编码的起源: 哈夫曼编码是 1952 年由 David A. Huffman 提出的一种无损数据压缩的编码算法.哈夫曼编码先统计出每种字母在字符串里出现的频率, ...
- 数据结构图文解析之:哈夫曼树与哈夫曼编码详解及C++模板实现
0. 数据结构图文解析系列 数据结构系列文章 数据结构图文解析之:数组.单链表.双链表介绍及C++模板实现 数据结构图文解析之:栈的简介及C++模板实现 数据结构图文解析之:队列详解与C++模板实现 ...
- HDU2527 哈夫曼编码
Safe Or Unsafe Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- YTU 3027: 哈夫曼编码
原文链接:https://www.dreamwings.cn/ytu3027/2899.html 3027: 哈夫曼编码 时间限制: 1 Sec 内存限制: 128 MB 提交: 2 解决: 2 ...
随机推荐
- OSC的原理
OSC是Online Schema Change简写,即在线架构改变.其实现步骤: 1. init,即初始化阶段,会对创建的表做一些验证工作,如检查表是否有主键,是否存在触发器或者外键等.2. cre ...
- [转]How to solve SSIS error code 0xC020801C/0xC004700C/0xC0047017
本文转自:http://www.codeproject.com/Articles/534651/HowplustoplussolveplusSSISpluserrorpluscodeplus0xC B ...
- iOS: 使用故事板和xib设置按钮圆角方法
使用storyboard如何设置圆角或边框? 通过storyboard的 运行时属性runtime attribute,可以对Button设置圆角或者边框 1.很多人都知道,通常设置一个 Button ...
- java中jar命令打包一个文件夹下的所有文件
(1)首先,必须保证java的所有路径都设置好,在dos提示符下输入jar -help出现C:\Documents and Settings\dly>jar -help非法选项:h用法:jar ...
- sqlmap原理及使用方法
1 . 介绍1.1 要求 1.2 网应用情节 1.3 SQL 射入技术 1.4 特点 1.5 下载和更新sqlmap 1.6 执照 2 . 用法2.1 帮助 2.2 目标URL 2.3 目标URL 和 ...
- scrapy-splash抓取动态数据例子一
目前,为了加速页面的加载速度,页面的很多部分都是用JS生成的,而对于用scrapy爬虫来说就是一个很大的问题,因为scrapy没有JS engine,所以爬取的都是静态页面,对于JS生成的动态页面都无 ...
- python处理csv
python处理csv 学习了:https://blog.csdn.net/qq_33363973/article/details/78783481 竟然pip install csv 无果: 学习 ...
- [Android Pro] 查看 keystore文件的签名信息 和 检查apk文件中的签名信息
1: 查看 keystore文件的签名信息 keytool -list -v -keystore keystoreName -storepass keystorePassword 2: 检查apk文件 ...
- [Angular] Two things about OnChanges Lifecycle hook
1. ngOnChanges is called before ngOnInit but after constructor() 2. ngOnChanges is called because of ...
- ssm整合(Spring+SpringMVC+Mybatis)
一.Spring Spring致力于提供一种方法管理你的业务对象.IOC容器,它可以装载bean(也就是我们java中的类,当然也包括service dao里面的),有了这个机制,我们就不用在每次使用 ...