hdu1053 Entropy
2015-11-30 19:56
295 查看
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstring>
using namespace std;
int a[30];
char s[1005];
struct node
{
int w;
friend bool operator < (node aa, node bb)
{
return aa.w > bb.w;
}
};
int main()
{
while(~scanf("%s", s))
{
if(!strcmp(s, "END")) break;
int len = strlen(s);
memset(a, 0, sizeof(a));
for(int i = 0; i < len; i++)
{
if(s[i] == '_') a[0] ++;
else a[s[i] - 'A' + 1] ++;
}
priority_queue <node> q;
for(int i = 0; i < 27; i++)
{
node b;
b.w = a[i];
if(a[i]) q.push(b);
}
int res;
if(q.size() == 1) res = len;
else
{
res = 0;
while(q.size() > 1)
{
int aa = q.top().w; q.pop();
int bb = q.top().w; q.pop();
res += (aa + bb);
node b;
b.w = aa + bb;
q.push(b);
}
}
printf("%d %d %.1lf\n", len*8, res, len*8.0/res);
}
return 0;
}
心得:啥也不说了。。。
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstring>
using namespace std;
int a[30];
char s[1005];
struct node
{
int w;
friend bool operator < (node aa, node bb)
{
return aa.w > bb.w;
}
};
int main()
{
while(~scanf("%s", s))
{
if(!strcmp(s, "END")) break;
int len = strlen(s);
memset(a, 0, sizeof(a));
for(int i = 0; i < len; i++)
{
if(s[i] == '_') a[0] ++;
else a[s[i] - 'A' + 1] ++;
}
priority_queue <node> q;
for(int i = 0; i < 27; i++)
{
node b;
b.w = a[i];
if(a[i]) q.push(b);
}
int res;
if(q.size() == 1) res = len;
else
{
res = 0;
while(q.size() > 1)
{
int aa = q.top().w; q.pop();
int bb = q.top().w; q.pop();
res += (aa + bb);
node b;
b.w = aa + bb;
q.push(b);
}
}
printf("%d %d %.1lf\n", len*8, res, len*8.0/res);
}
return 0;
}
心得:啥也不说了。。。
相关文章推荐
- 各种软件架构风格
- Linux第三次实验报告
- NGINX部署项目后浏览器选项卡icon图标不显示
- 【c#】—架构函数&&析构函数
- shell笔记-local、export用法 、declare、set
- linux下自动挂载samba目录
- Properties--配置文件的加载
- 快速回忆Apache+svn新增用户
- OpenGrok介绍
- OpenGrok安装配置
- hadoop中HDFS文件系统常用命令:
- hadoop中HDFS文件系统常用命令:
- 阿里云主机免费申请级网站配置
- OpenCV笔记(三)
- 阿里云主机免费申请级网站配置
- 【OpenCV学习笔记】2.1图像混合和叠加
- centos反编译APK包
- centos反编译APK包
- 嵌入式Linux-objdump命令的使用
- 浅析 Linux 初始化 init 系统,第 3 部分: Systemd