poj - 1521 Entropy

Huffman的题,基本都是用pq水过。今天我才知道,计算压缩后编码总长度,只要用加的方法就行了。

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <queue>
 4 using namespace std;
 5 int main()
 6 {
 7     char ss[1000];
 8     int i,l,n,a,b;
 9     int d[40];
10     while(scanf("%s",ss),strcmp(ss,"END"))
11     {
12         memset(d,0,sizeof d);
13         l = strlen(ss);
14         for(i = 0; i < l; i++)
15             d[ss[i]-60]++;
16         priority_queue<int,vector<int>,greater<int> >q;
17         for(i = 0; i < 40; i++)if(d[i])
18             q.push(d[i]);
19         n = 0;
20         if(q.size()==1)
21         {
22             a = q.top();
23             printf("%d %d 8.0\n",8*a,a);
24             continue;
25         }
26         while(q.size()>1)
27         {
28             a = q.top(); q.pop();
29             b = q.top(); q.pop();
30             q.push(a+b);
31             n += a+b;
32         }
33         printf("%d %d %.1lf\n",8*l,n,8*l/double(n));
34     }
35     return 0;
36 }

转载于:https://www.cnblogs.com/lzxskjo/archive/2012/08/20/2647807.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值