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个字母的情况
- hdu/hdoj 1053 Entropy
- HDOJ 1053 Entropy
- Hdoj 1053 Entropy 【STL】
- hdoj 1053 Entropy【基础】
- HDOJ 1053 Entropy 哈夫曼编码
- hdoj 1053 Entropy 【huffman编码】
- Hdu 1053 Entropy
- HDU 1053 Entropy
- hdu 1053 Entropy
- hdu 1053 Entropy
- hdu Entropy 1053 哈夫曼树
- hdu 1053 Entropy
- hdu 1053 Entropy
- hdu 1053Entropy
- HDU - 1053 Entropy
- hdu 1053 Entropy(霍夫曼树)
- HDU 1053 Entropy
- HDU 1053 Entropy
- PostgreSQL View the table structure
- Log4net
- 自定义的二级下拉控件
- ORA-15025, ORA-27041
- 编程之美2.4 求1的个数
- hdu/hdoj 1053 Entropy
- Defunct进程 僵尸进程
- Xml服务器验证和国际化及本地化
- 提醒自己的一篇博文
- Exercises 2.2 E2(b)解答
- [转]Android的Handler总结
- github简单使用教程
- LaTeX之用xy-pic包画交换图
- NULL指针引起的一个linux内核漏洞