hdu/hdoj 1053 Entropy

来源:互联网 发布:林弯弯淘宝店铺 编辑:程序博客网 时间:2024/05/01 00:22

题目大意:给你一个字符串,每一个字符在计算机中用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个字母的情况

 

 

原创粉丝点击