POJ 1521 Entropy
来源:互联网 发布:淘宝男装店铺排名 编辑:程序博客网 时间:2024/05/16 08:28
霍夫曼树
#include<cstdio>#include<string.h>#include<iostream>#include<string>using namespace std;#define LEFT(i) 2*i#define RIGHT(i) 2*i+1#define PARENT(i) i/2int heapsize,root;int finalcode[200];struct Word{int num,number;double freq;}word[200];struct Node{int num,l,r;bool leaf;}node[200];void exchange(int s,int t){int tem1;double tem2;int tem3;tem1=word[s].num;tem2=word[s].freq;tem3=word[s].number;word[s].num=word[t].num;word[s].freq=word[t].freq;word[s].number=word[t].number;word[t].num=tem1;word[t].freq=tem2;word[t].number=tem3;}void min_heap(int i){int l,r,largest;l=LEFT(i);r=RIGHT(i);if(l>heapsize && r>heapsize)return;if(l<=heapsize && word[l].freq<word[i].freq)largest=l;elselargest=i;if(r<=heapsize && word[r].freq<word[largest].freq)largest=r;if(largest!=i){exchange(i,largest);min_heap(largest);}}void init_heap(){int i;for(i=heapsize/2;i;i--){min_heap(i);}}void insert(){exchange(1,heapsize);min_heap(1);}void clear(){exchange(1,heapsize);heapsize--;min_heap(1);}void addedge(int parent,int l,int r,int lmark,int rmark){node[parent].leaf=0;node[parent].l=lmark;node[parent].r=rmark;if(l!=-1){node[lmark].leaf=1;node[lmark].num=l;}if(r!=-1){node[rmark].leaf=1;node[rmark].num=r;}}void code(int now,int i){int j;if(node[now].leaf){finalcode[node[now].num]=i;return;}code(node[now].l,i+1);code(node[now].r,i+1);}int main(){char temp;int i,times[200];int totalwords,nodes;string str;while(cin>>str && !(str=="END")){memset(times,0,sizeof(times));totalwords=0;for(i=0;i<str.size();i++){temp=str[i];times[temp]++;totalwords++;}heapsize=0;for(i=0;i<=130;i++){if(times[i]){word[++heapsize].number=-1;word[heapsize].num=i;word[heapsize].freq=(double)times[i]/totalwords;}}nodes=0;init_heap();while(heapsize>1){int l,r,lmark,rmark;double fl,fr;l=word[1].num;fl=word[1].freq;if(word[1].number==-1){++nodes;lmark=nodes;}elselmark=word[1].number;clear();r=word[1].num;fr=word[1].freq;if(word[1].number==-1){++nodes;rmark=nodes;}elsermark=word[1].number;clear();word[++heapsize].freq=fl+fr;word[heapsize].num=-1;word[heapsize].number=++nodes;insert();addedge(nodes,l,r,lmark,rmark);}root=nodes;if(nodes)code(root,0);elsefinalcode[word[1].num]=1;int total1=0,total2=0;for(i=0;i<=130;i++){if(times[i]){total1+=times[i]*8;total2+=times[i]*finalcode[i];}}printf("%d %d %.1f\n",total1,total2,(double)total1/total2);}}
- POJ 1521 Entropy
- poj 1521 Entropy
- poj 1521 Entropy
- POJ 1521 Entropy
- POJ 1521 Entropy
- POJ - 1521 Entropy
- poj 1521 Entropy
- POJ 1521 , Entropy , Huffman
- POJ 1521-Entropy 贪心问题
- POJ 1521 Entropy(哈夫曼树)
- poj 1521(ENTROPY) huffman 编码
- POJ-1521/ZOJ-1117/Entropy
- 贪心1003 POJ 1521-Entropy
- hdu-1053-Entropy && poj-1521-Entropy (哈夫曼编码)
- POJ 1521 Entropy(哈夫曼编码)
- poj 1521 Entropy huffman(哈夫曼)编码
- poj 1521 Entropy(优先队列)
- POJ 1521 Entropy 优先队列/multiset
- 游戏删了
- C++程序设计实验报告(六十三)---第十周任务2
- LINUX阶段总结
- pipe
- C/C++代码分析时的一些反汇编方法
- POJ 1521 Entropy
- vc之button控件
- JNI 调用 DLL
- PCB布局布线的一些常用的规则
- IO流学习分享
- 数据结构之Treap
- BrainFuck 解释器(C语言实现)
- MongoDB入门
- 第三天 CSDN (数组、Ajax技术、PHP)