http://acm.hdu.edu.cn/showproblem.php?pid=1053

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue> using namespace std; int a[]; char s[]; int cal(char x){
if(x == '_') return ;
else return x - 'A' + ;
} struct node{
int w;
friend bool operator <(node aa, node bb){
return aa.w > bb.w;
}
}; int main(){
while(~scanf("%s", s)){
if(!strcmp(s,"END")) break;
int len = strlen(s);
memset(a, , sizeof(a));
for(int i = ; i < len; i++){
a[cal(s[i])]++;
}
priority_queue <node> q;
for(int i = ; i < ; i++){
node b;
b.w = a[i];
if(a[i]) q.push(b);
}
int res;
if(q.size() == ) res = len;
else{
res = ;
while(q.size() > ){
int aa = q.top().w; q.pop();
int bb = q.top().w; q.pop();
res += (aa + bb);
node b;
b.w = aa + bb;
q.push(b);
}
}
printf("%d %d %.1lf\n", len*, res, len*8.0/res);
}
return ;
}

http://acm.hdu.edu.cn/showproblem.php?pid=2527

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue> using namespace std; int a[]; char s[]; int cal(char x){
return x - 'a' + ;
} struct node{
int w;
friend bool operator <(node aa, node bb){
return aa.w > bb.w;
}
}; int main(){
int n;
scanf("%d", &n);
while(n--){
int m;
scanf("%d %s", &m, s);
int len = strlen(s);
memset(a, , sizeof(a));
for(int i = ; i < len; i++){
a[cal(s[i])]++;
}
priority_queue <node> q;
for(int i = ; i < ; i++){
node b;
b.w = a[i];
if(a[i]) q.push(b);
}
int res;
if(q.size() == ) res = len;
else{
res = ;
while(q.size() > ){
int aa = q.top().w; q.pop();
int bb = q.top().w; q.pop();
res += (aa + bb);
node b;
b.w = aa + bb;
q.push(b);
}
}
if(res <= m) puts("yes");
else puts("no");
}
return ;
}

两道几乎相同的题,哈夫曼编码的长度是合出来的各个节点之和(只剩一个节点停止),开始每个节点的权值是字符出现的频率,如果开始只有一个节点的情况要特判

HDU 1053 & HDU 2527 哈夫曼编码的更多相关文章

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

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

  2. hdoj 1053 Entropy(用哈夫曼编码)优先队列

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1053 讲解: 题意:给定一个字符串,根据哈夫曼编码求出最短长度,并求出比值. 思路:就是哈夫曼编码.把 ...

  3. HDU2527 哈夫曼编码

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

  4. hdu2527哈夫曼编码

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

  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. *HDU1053 哈夫曼编码

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

  9. YTU 3027: 哈夫曼编码

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

随机推荐

  1. <mvc:annotation-driven />注解意义

    <mvc:annotation-driven /> 是一种简写形式,完全可以手动配置替代这种简写形式,简写形式可以让初学都快速应用默认配置方案. <mvc:annotation-dr ...

  2. 《转》---使用递归方法DataTable 绑定 TreeView

    转自:http://blog.sina.com.cn/s/blog_8944756d01016yaj.html 前台: <asp:View ID="view0" runat= ...

  3. 循环语句while与for的穷举迭代

    循环语句while while当...的时候 int n=1; while(n<6)//在括号内直接限制逻辑关系 {//需要在大括号内给出改变方式,否则将进入死循环 console.WriteL ...

  4. 字符串匹配的sunday算法

    sunday算法核心思想:启发式移动搜索步长! SUNDAY 算法描述: 字符串查找算法中,最著名的两个是KMP算法(Knuth-Morris-Pratt)和BM算法(Boyer-Moore).这里介 ...

  5. java网络编程serversocket

    转载:http://www.blogjava.net/landon/archive/2013/07/24/401911.html Java网络编程精解笔记3:ServerSocket详解ServerS ...

  6. dll和ocx比较

    ActiveX,OLE是基于COM的一种应用,其文件后缀一般以dll和ocx结尾:ocx作为一种特殊的dll文件,具有一定的用户界面和事件响应,而dll文件只是方法和属性的集合. 一.关于DLL的介绍 ...

  7. Graphical installers are not supported by the vm

    http://www-01.ibm.com/support/docview.wss?uid=swg21462180 Technote (troubleshooting) Problem(Abstrac ...

  8. 无需激活用户直接登入discuz

    //打开discuz/api/uc.php //synlogin方法(180行)处,往下找到 if(($member = getuserbyuid($uid, 1))) { dsetcookie('a ...

  9. mybatis中的mapper.xml

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-/ ...

  10. qml json 解析到 ListView

    https://github.com/kromain/qml-utils/tree/master/JSONListModel 非常棒!! 实现的原理如下文: http://goessner.net/a ...