ACM--Entrophy(优先队列建树+哈希)

//主要感悟:
//建哈夫曼树使用优先队列能够显著减少代码量,也比较容易实现。
//建立一个哈希表,每个字符与一个数字对应,这样编程比较方便

#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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值