UVALIVE 2088 Entropy Huffman

直接优先队列模拟就可以了,上代码,这里q.size()==1WA了一次,直接只想到len==1的特殊情况

#include <map>
#include <set>
#include <list>
#include <cmath>
#include<cctype>
#include <ctime>
#include <deque>
#include <stack>
#include <queue>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define PI 3.1415926535897932626
using namespace std;
int gcd(int a, int b)
{
	return a % b == 0 ? b : gcd(b, a % b);
}
#define MAXN 5005
int vis[30];
priority_queue<int,vector<int> ,greater<int> >q;
char str[MAXN];
int len;
void init()
{
    memset(vis,0,sizeof(vis));
    while (!q.empty()) q.pop();
    len=strlen(str);
    for (int i=0;i<len;i++)
    {
        if (str[i]=='_') vis[26]++;
        else vis[str[i]-'A']++;
    }
    for (int i=0;i<=26;i++)
      if (vis[i])  q.push(vis[i]);
}
void slove()
{
    int ans=0;
    while (!q.empty())
    {
        int t1=q.top();q.pop();
        if (q.empty()) break;
        int t2=q.top();q.pop();
        ans+=t1+t2;
        q.push(t1+t2);
    }
    printf("%d %d %.1f\n",8*len,ans,(double)8*len/ans);
}
int main()
{
   // freopen("sample.txt","r",stdin);
    while (scanf("%s",str)!=EOF)
    {
        if (strcmp(str,"END")==0) break;
        init();
        if (q.size()==1) {printf("%d %d 8.0\n",len*8,len);continue;}
        slove();
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值