ACM--Entrophy(优先队列建树+哈希)
来源:互联网 发布:网络打击赌博最新新闻 编辑:程序博客网 时间:2024/06/05 16:29
//主要感悟:
//建哈夫曼树使用优先队列能够显著减少代码量,也比较容易实现。
//建立一个哈希表,每个字符与一个数字对应,这样编程比较方便
#include <iostream>#include<queue>#include<stdio.h>#include<string.h>#include<iomanip>using namespace std;struct Tree{int parent;int weight; int order; int code_lenth;}a[100];struct cmp{ bool operator ()(Tree &a,Tree &b){ return a.weight>b.weight; }};int hash[200];int main(){priority_queue<Tree,vector<Tree>,cmp >q;string n;while(getline(cin,n),n!="END"){ int lenth=n.size();int j=1;//序号 memset(hash,0,sizeof(hash));a[1].order=j;a[1].weight=1; hash[n[0]]=j;for(int i=1;i<lenth;i++){ int k=hash[n[i]]; //cout<<k<<" "<<j<<endl; if(k)a[k].weight++;else{a[++j].weight=1;a[j].order=j;hash[n[i]]=j;}}for(int i=1;i<=j;i++) q.push(a[i]); for(int i=j+1;i<=2*j-1;i++){ Tree temp;Tree temp1;Tree temp2;temp1=q.top();q.pop();temp2=q.top();q.pop();a[temp1.order].parent=i;a[temp2.order].parent=i;temp.weight=temp1.weight+temp2.weight;temp.order=i;temp.parent=0;a[i]=temp;q.push(temp);}q.pop();for(int i=1;i<=j;i++){ int sum=0; int k=i;while(a[k].parent){sum++;k=a[k].parent;}a[i].code_lenth=sum;}int sum_lenth=0;for(int i=0;i<lenth;i++){int k=hash[n[i]];sum_lenth+=a[k].code_lenth;}if(j==1) sum_lenth=lenth;cout<<lenth*8<<" "<<sum_lenth<<" "; cout<<fixed<<setprecision(1)<<(float)lenth*8/(float)sum_lenth<<endl;}return 0;}
0 0
- ACM--Entrophy(优先队列建树+哈希)
- ACM--Entrophy(哈希+数组 建树)
- 小学生玩ACM----优先队列
- ACM 优先队列和堆
- [ACM] hdu 1242 Rescue (BFS+优先队列)
- [ACM] HDU 1242 Rescue (优先队列)
- HDU ACM 4546 比赛难度->优先队列
- [ACM] HDU 1242 Rescue (优先队列)
- [ACM] hdu 1242 Rescue (BFS+优先队列)
- 杭电 acm 幸福列车 (优先队列 以及优先队列中的优先项如何排列)
- ACM 模板 c++ STL 队列 queue 优先队列
- 四川省ACM省赛B题,优先队列+BFS Charitable Exchange
- [ACM] POJ 1442 Black Box (堆,优先队列)
- HDU ACM 1873 看病要排队->优先队列
- ACM暑期集训——专题一[优先队列]
- 挑战ACM迷宫(DFS,BFS,BFS+优先队列)
- 优先队列详解+ACM例题分析(转载)
- ACM算法:深度优先搜索(附剪枝和优先队列的说明)
- 浅谈jsopn跨域请求原理及cors(跨域资源共享)解决方案
- 参数类型对java用继承实现多态时的影响
- hdu 5285 wyh2000 and pupil
- Android问题集锦之二十八:You need to use a Theme.AppCompat theme (or descendant) with this activity.
- UEFI简单的应用程序模块MyHelloWorld:C源文件/INF工程文件源码/简析[6]
- ACM--Entrophy(优先队列建树+哈希)
- JAVA爬虫WebCollector
- Java数据结构-线性表之单链表LinkedList
- finally代码块中的代码什么时候执行
- brophp写商品管理模块
- 集合特点
- 所闻所获2:使用块回调来实现代理的功能
- 【linux下编程】MakeFile
- .Net知识点(基础加强)