题目链接: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;
}
}

最新文章

  1. 用FileInputStream读取数据,计算机如何实现将两个字节拼接成中文的?
  2. Building,Packaging,Deploying,and Administering Applications and Types
  3. UIView的frame和bounds的含义
  4. jquery-2.0.3.js和jquery-2.0.3.min.js的区别
  5. TCP/IP 教程
  6. 纯CSS实现各类气球泡泡对话框效果
  7. 《java入门第一季》之Arrays类
  8. 安利一个刚考过的信息安全认证Security+
  9. Scala数组| 集合
  10. Python2入门(1)
  11. nginx配置备忘
  12. jenkins编译jar包 报connection连接错误
  13. 这套方法论,彻底终结MySQL同步延迟问题
  14. usb_ctrl
  15. hdu 1.2.8
  16. Zabbix之Python发送邮件
  17. 数据导入报错:Got a packet bigger than‘max_allowed_packet’bytes的问题
  18. PHP使用FPDF pdf添加水印中文乱码问题 pdf合并版本问题
  19. xt
  20. Hive Tuning(一) 连接策略

热门文章

  1. Android内存优化11 内存泄漏常见情况2 内部类泄漏
  2. Windows命令行的使用
  3. synchronized 线程同步
  4. 二十四种设计模式:建造者模式(Builder Pattern)
  5. hdu 4893Wow! Such Sequence!
  6. MyEclipse 2014配置Maven
  7. win下idea远程提交WordCount任务到HA集群
  8. JDK自带内存及线程分析工具
  9. MySQL外键及级联删除 &amp;&amp; 表的存储引擎与创建索引 &amp;&amp; 删除数据库和表
  10. PHP面向对象之接口 (interface)