题目链接: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(用哈夫曼编码)优先队列的更多相关文章

  1. HDU 1053 Entropy(哈夫曼编码 贪心+优先队列)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1053 Entropy Time Limit: 2000/1000 MS (Java/Others)   ...

  2. HDU 1053 & HDU 2527 哈夫曼编码

    http://acm.hdu.edu.cn/showproblem.php?pid=1053 #include <iostream> #include <cstdio> #in ...

  3. *HDU1053 哈夫曼编码

    Entropy Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  4. 图像压缩编解码实验(DCT编码+量化+熵编码(哈夫曼编码))【MATLAB】

    课程要求 Assignment IV Transform + Quantization + Entropy Coding Input: an intra-frame or a residue pict ...

  5. 哈夫曼(huffman)树和哈夫曼编码

    哈夫曼树 哈夫曼树也叫最优二叉树(哈夫曼树) 问题:什么是哈夫曼树? 例:将学生的百分制成绩转换为五分制成绩:≥90 分: A,80-89分: B,70-79分: C,60-69分: D,<60 ...

  6. (转载)哈夫曼编码(Huffman)

    转载自:click here 1.哈夫曼编码的起源: 哈夫曼编码是 1952 年由 David A. Huffman 提出的一种无损数据压缩的编码算法.哈夫曼编码先统计出每种字母在字符串里出现的频率, ...

  7. 数据结构图文解析之:哈夫曼树与哈夫曼编码详解及C++模板实现

    0. 数据结构图文解析系列 数据结构系列文章 数据结构图文解析之:数组.单链表.双链表介绍及C++模板实现 数据结构图文解析之:栈的简介及C++模板实现 数据结构图文解析之:队列详解与C++模板实现 ...

  8. HDU2527 哈夫曼编码

    Safe Or Unsafe Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  9. YTU 3027: 哈夫曼编码

    原文链接:https://www.dreamwings.cn/ytu3027/2899.html 3027: 哈夫曼编码 时间限制: 1 Sec  内存限制: 128 MB 提交: 2  解决: 2 ...

随机推荐

  1. Boost::Lexical_cast 的使用

    .C++代码 #include <boost/lexical_cast.hpp> #include <iostream> int main() { using boost::l ...

  2. 用latex写毕业论文

    用 LaTeX 写漂亮学位论文(from wloo) 序 一直觉得有必要写这样一篇文章,因为学位论文从格式上说更像一本书,与文章 的排版不同,不仅多出目录等文章没有的部分,而且一般要设置页眉页脚方便阅 ...

  3. 二八定律全面分析SEO全过程

    首先,大体的了解SEO的整个流程,SEO的整个流程无非是:关键词选定.标题标签描述的正确描写---内部优化----外链----内容---友情链接---更新. 在SEO界流行这样一句话:“内容为王,外链 ...

  4. 远程操作linux

    import java.io.IOException;import java.io.InputStream;import org.apache.commons.io.IOUtils; import c ...

  5. EasyUI-子页面增加显示tabs的一个问题

    在父页面点个链接能动态看到子页面的情况太简单,请看easyUI官网:http://www.jeasyui.com/tutorial/layout/tabs2.php现在说的是在子页面点个按钮也能触发增 ...

  6. stderr重定向

    在测试alljoyn时开启了内部日志,输出太多想重定向到文件中,使用如下命令: ./chat -s aaa >.txt 居然还是打印到屏幕上而不是输出到文件中. 查看alljoyn写日志的代码, ...

  7. centos6.8服务器配置之编译安装PHP、配置nginx

    php version 5.6.31.nginx version: nginx/1.10.2 1.下载: wget http://cn2.php.net/distributions/php-5.6.3 ...

  8. [ES6] 10. Array Comprehensions

    ES6 decided that Array Comprehensions will not included in this version, ES7 will include this. Ther ...

  9. 在 TDA 工具里看到 Java Thread State 的第一反应是

    转载:http://itindex.net/detail/43158-tda-%E5%B7%A5%E5%85%B7-java   使用 TDA 工具,看到大量 Java Thread State 的第 ...

  10. Node.js 2017.11.5-2017.11.16期间制作的图片爬虫总结

    2017年11月18日12:33:06