hdoj 1053 Entropy(用哈夫曼编码)优先队列
2024-10-21 07:55:35
题目链接: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;
}
}
最新文章
- 用FileInputStream读取数据,计算机如何实现将两个字节拼接成中文的?
- Building,Packaging,Deploying,and Administering Applications and Types
- UIView的frame和bounds的含义
- jquery-2.0.3.js和jquery-2.0.3.min.js的区别
- TCP/IP 教程
- 纯CSS实现各类气球泡泡对话框效果
- 《java入门第一季》之Arrays类
- 安利一个刚考过的信息安全认证Security+
- Scala数组| 集合
- Python2入门(1)
- nginx配置备忘
- jenkins编译jar包 报connection连接错误
- 这套方法论,彻底终结MySQL同步延迟问题
- usb_ctrl
- hdu 1.2.8
- Zabbix之Python发送邮件
- 数据导入报错:Got a packet bigger than‘max_allowed_packet’bytes的问题
- PHP使用FPDF pdf添加水印中文乱码问题 pdf合并版本问题
- xt
- Hive Tuning(一) 连接策略
热门文章
- Android内存优化11 内存泄漏常见情况2 内部类泄漏
- Windows命令行的使用
- synchronized 线程同步
- 二十四种设计模式:建造者模式(Builder Pattern)
- hdu 4893Wow! Such Sequence!
- MyEclipse 2014配置Maven
- win下idea远程提交WordCount任务到HA集群
- JDK自带内存及线程分析工具
- MySQL外键及级联删除 &;&; 表的存储引擎与创建索引 &;&; 删除数据库和表
- PHP面向对象之接口 (interface)