求哈弗曼编码
来源:互联网 发布:智慧树网络课程好过吗? 编辑:程序博客网 时间:2024/06/03 16:06
/*
给你n个数,让你构建一个哈弗曼树,求哈弗曼编码的长度
解:就是求除了叶子节点以外的所有节点的权值和
*/
#include<iostream>
#include<queue>
using namespace std;
typedef long long inta;
struct node
{
inta weight;
bool operator<(const node & a) const
{
return weight>a.weight;
}
};
int main()
{
int size,n,l;
while(cin>>n)
{
priority_queue<node> pq;
long long ans=0;
for(int i=0;i<n;i++)
{
int w;
cin>>w;
node temp;
temp.weight=w;
pq.push(temp);
}
while(pq.size()>1)
{
node n1=pq.top();
pq.pop();
node n2=pq.top();
pq.pop();
node newnode;
newnode.weight=n1.weight+n2.weight;
pq.push(newnode);
ans+=newnode.weight;
}
cout<<ans<<endl;
}
}
给你n个数,让你构建一个哈弗曼树,求哈弗曼编码的长度
解:就是求除了叶子节点以外的所有节点的权值和
*/
#include<iostream>
#include<queue>
using namespace std;
typedef long long inta;
struct node
{
inta weight;
bool operator<(const node & a) const
{
return weight>a.weight;
}
};
int main()
{
int size,n,l;
while(cin>>n)
{
priority_queue<node> pq;
long long ans=0;
for(int i=0;i<n;i++)
{
int w;
cin>>w;
node temp;
temp.weight=w;
pq.push(temp);
}
while(pq.size()>1)
{
node n1=pq.top();
pq.pop();
node n2=pq.top();
pq.pop();
node newnode;
newnode.weight=n1.weight+n2.weight;
pq.push(newnode);
ans+=newnode.weight;
}
cout<<ans<<endl;
}
}
0 0
- 求哈弗曼编码
- 构造哈夫曼树 求哈弗曼编码
- 编码
- 编码
- 编码
- 编码
- 编码
- 编码
- 编码
- 编码
- 编码
- 编码
- 编码
- 编码
- 编码
- 编码
- 编码
- 编码
- canada goose parka-714
- LeetCode-Subsets
- Python replace()方法
- UNP卷1:第十四章(高级I/O函数)
- LaTeX完整例子_参考文献、图、表和公式
- 求哈弗曼编码
- hdu 1874 dijsktra(mlogn) +bellmanford
- nginx send back content post by client
- 设置字体相关命令
- <图形图像,动画,多媒体> 读书笔记 --- 音效
- [HDU 4776 Ants] Trie异或+预处理
- 寻找阿里巴巴:杨致远完成硅谷史上最赚钱赌博
- 【无限互联】SDWebImage图片缓存流程分析
- c++命令提示符窗口下打印指定大小的菱形代码