hdu/hdoj 1053 Entropy

题目大意:给你一个字符串,每一个字符在计算机中用8位编码表示,所以给你字符串AAAAABCD,它在计算机中要占用64位,可是为了节省内存,希望你能
设计出一种编码,使得内存的占用尽可能的少,即字符串的位数表示最少,输出  原本占几位,编码后占几位,二者的商

 

 

#include <iostream>
#include <queue>
#include <vector>
#include <stdio.h>
#include <string>
#include <cstring>
using namespace std;

struct kiss
{
    int kk;
    bool operator < (const kiss &a)
    {
        return kk>a.kk;
    }
}tmep[27];
priority_queue<int,vector<int>,greater<int> > que;
int main()
{
    string str;
    
    while (getline(cin,str),str!="END")
    {
        
        
        for (int i=0; i<27; ++i)
        {
            tmep[i].kk=0;
        }
        while(!que.empty())
            que.pop();

        for (int i=0; i<str.length(); ++i)
        {
            if (str[i]=='_')
                tmep[26].kk++;
            else
                tmep[str[i]-'A'].kk++;
        }

        for (int i=0; i<27; ++i)
        {
            if (tmep[i].kk!=0)
            {
                que.push(tmep[i].kk);
            }
        }
        int ans=0,a,b;
        if (que.size()==1)
        {
            
            que.pop();
            
            printf("%d %d 8.0\n",str.length()*8,str.length());
        }


        else
        {
            
        
            while(que.size()!=1)
            {
                a=que.top();que.pop();
                b=que.top();que.pop();
                  que.push(a+b);
                  ans+=(a+b);
                }
            printf("%d %d %.1lf\n",str.length()*8,ans,str.length()*8.0/ans);
        }
        
    
    }
}


用优先队列实现,方便的很,可能需要注意只有1个字母的情况

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值