Entropy (huffman) 优先队列)
http://acm.hdu.edu.cn/showproblem.php?pid=1053
Huffman问题利用STL中的priority_queue解决;
#include<stdio.h>
#include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<queue>
using namespace std; struct cmp
{
bool operator()(const int a, const int b)
{
return a > b;
}
};
int solve(string str)
{
priority_queue< int,vector<int>,cmp >que;
map<char, int> mymap;
int i;
for(i =; i < str.size(); i++)
{
mymap[str[i]]++;
} map<char,int>::iterator it = mymap.begin();
while(it != mymap.end())
{
//printf("%c %d\n",it->first,it->second);
que.push(it->second);
it++;
} int weight = ;
if(que.size() == )
return que.top(); while(que.size() != )
{
int a,b;
a = que.top();
que.pop();
b = que.top();
que.pop(); weight += a+b;
que.push(a+b);
}
return weight;
}
int main()
{
int weight;
string str;
while(cin>>str)
{
if(str == "END")
break;
weight = solve(str);
printf("%d %d %.1lf\n",*str.size(),weight,8.0*str.size()/weight);
}
return ;
}
Entropy (huffman) 优先队列)的更多相关文章
- UVa 10954 (Huffman 优先队列) Add All
直接用一个优先队列去模拟Huffman树的建立过程. 每次取优先队列前两个数,然后累加其和,把这个和在放入到优先队列中去. #include <cstdio> #include <q ...
- uvalive 2088 - Entropy(huffman编码)
题目连接:2088 - Entropy 题目大意:给出一个字符串, 包括A~Z和_, 现在要根据字符出现的频率为他们进行编码,要求编码后字节最小, 然后输出字符均为8字节表示时的总字节数, 以及最小的 ...
- 哈夫曼编码(Huffman coding)的那些事,(编码技术介绍和程序实现)
前言 哈夫曼编码(Huffman coding)是一种可变长的前缀码.哈夫曼编码使用的算法是David A. Huffman还是在MIT的学生时提出的,并且在1952年发表了名为<A Metho ...
- 优先队列实现Huffman编码
首先把所有的字符加入到优先队列,然后每次弹出两个结点,用这两个结点作为左右孩子,构造一个子树,子树的跟结点的权值为左右孩子的权值的和,然后将子树插入到优先队列,重复这个步骤,直到优先队列中只有一个结点 ...
- 优先队列求解Huffman编码 c++
优先队列小析 优先队列的模板: template <class T, class Container = vector<T>,class Compare = less< ...
- Huffman树编码-优先队列实现
Huffman编码是之前一道算法作业题,最近又要复习考试了,先把这个的代码再看一下吧. 算法原理很简单,使用优先队列将两个节点弹出,然后合并节点之后再入队列如此循环做下去即可. 主要问题在于树的修改问 ...
- HDU 1053 Entropy(哈夫曼编码 贪心+优先队列)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1053 Entropy Time Limit: 2000/1000 MS (Java/Others) ...
- [ACM] POJ 3253 Fence Repair (Huffman树思想,优先队列)
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 25274 Accepted: 8131 Des ...
- poj 3253 Fence Repair(优先队列+huffman树)
一个很长的英文背景,其他不说了,就是告诉你锯一个长度为多少的木板就要花多少的零钱,把一块足够长(不是无限长)的木板锯成n段,每段长度都告诉你了,让你求最小花费. 明显的huffman树,优先队列是个很 ...
随机推荐
- (转载)linux那点事儿(中)
原文地址:http://www.cnblogs.com/fnng/archive/2012/03/19/2407162.html 本文只是转载供自己学习之用 2012-03-22 13:31 by 虫 ...
- phpnow安装教程
点评:搭建 PHP 其实不很难,只是有点繁琐.要是自己搭建一次 PHP + MySQL 环境很是费时.更糟的是,很多新手在配置 PHP 时常常出现这样那样的问题.诸如 mysql 扩展.zend 安装 ...
- 外观模式-Facade
外观模式是为了解决类与类之间依赖关系的,外观模式将类间关系放在一个Facade类中,降低了类类之间的耦合度,该模式中不涉及接口 举一个经典的例子: CPU类: public class CPU { p ...
- mac 下svn降级
mac 手欠 homebrew 安装完成后 brew install svn svn版本更新至1.8.11 公司svn 不支持1.8 需要降级 搜索很多资料 写的比较麻烦 总结出来是先卸载再 ...
- frame模型
#import "CZWeiboFrame.h" #import "CZWeibo.h" @implementation CZWeiboFrame #defin ...
- 怎样使用svn开发项目
那么首先什么是svn呢?官方有很好的解释,我说一下个人简单的理解,svn就是开源的版本控制软件, 那么什么是版本呢?简单的说版本就是标记,比如你买了一本书,同样的书名,但是版本不一定一样, 因为里面可 ...
- maven jetty运行命令
1.先运行build.xml <?xml version="1.0" encoding="UTF-8"?> <project name=&qu ...
- Use StringBuilder instead of String as possible as you can.
If you are care a littile about the time your algorithm cost,you should notice that,you my use Strin ...
- SQLite 入门教程(二)创建、修改、删除表
一.数据库定义语言 DDL 在关系型数据库中,数据库中的表 Table.视图 View.索引 Index.关系 Relationship 和触发器 Trigger 等等,构成了数据库的架构 Schem ...
- oracle中所有关于时间日期的问题总结
select current_date as 当前会话时间,sysdate as 系统时间, systimestamp as 系统详细时间 from dual;