题目链接: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. activiti源码笔记对标的博客

    http://blog.csdn.net/bluejoe2000/article/category/2812511/2 白乔专栏 http://blog.csdn.net/bluejoe2000/ar ...

  2. 在JAVA中,如何计算两个日期的月份差

    package com.forezp.util; import org.joda.time.DateTime; import org.joda.time.Months; import org.joda ...

  3. jquery next()方法

    1.html代码 <!DOCTYPE html> <html> <head> <script type="text/javascript" ...

  4. Android检测网络连接

    Android检测网络连接 import android.app.AlertDialog; import android.content.Context; import android.content ...

  5. 如何理解redo和undo

    redo和undo的区别    redo--> undo-->datafileinsert一条记录时, 表跟undo的信息都会放进 redo 中, 在commit 或之前, redo 的信 ...

  6. python Parent.__init()和super(Child, self)的区别

    super函数做的事情 def supper(cls, inst): mro = inst.__class__.mro() return mro[mro.index(cls) + 1] inst生成父 ...

  7. webDriver API——第5部分Special Keys

    The Keys implementation. class selenium.webdriver.common.keys.Keys Bases: object Set of special keys ...

  8. js 创建数组方法以及区别

    示例代码: <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF ...

  9. 利用POI进行Excel的导出

    需求:将用户的违约金信息导出为excel表格格式 步骤 1. 数据库中增加按钮的值(注意上级编号要和页面隐藏域中的相等) DZ内容(页面加载时根据SJBH查询数据库内容,读取DZ字段信息并加载样式及方 ...

  10. HTML5&amp;CSS3初学者指南

    介绍 网络时代已经到来.现在对人们来说,每天上网冲浪已经成为一种最为常见的行为. 一个典型的网页是由文本.图像和链接组成的.除去内容上的差异,不同网站的网页也具有不同的外观和感受,以实现在网络上建立自 ...