标签:
/*
解题思路用的是哈夫曼求总值+优先队列,对应的解题思路链接:http://wenku.baidu.com/link?url=O1dy_VtmtDwIFLs_p419lbxq3BDoXFal5uNKr_pBUJeeihm-6j3vbmUqga2KDY5VHG2yQN7eBR4cc1aueouWnvtTPHOldyEIMBHIBI8BNg_
网友的解题代码链接:http://blog.csdn.net/wchyumo2009/article/details/7426421
我的代码根据以上网友的代码稍微改进了一点。但是思路是一模一样的
*/
#include <string>
#include <algorithm>
using namespace std;
int num[27];
void init()
{
int i;
for(i = 0; i < 27; i ++){
num[i] = 0;
}
}
int fun()
{
int n = 0,i,j,l;
sort(num, num+27);
int tmp[27],k = 0;
for(i = 0; i < 27; i ++){
if(num[i] != 0)break;
}
l = 27-i;
if(l == 1)return num[26];
for(i = 27-l; i < 27; i ++){
tmp[k++] = num[i];
// printf("tmp[%d] = %d\n", k-1, tmp[k-1]);
}
for(i = 0; i < l - 1; i ++){
tmp[0] = tmp[0] + tmp[1];
n+=tmp[0];
for(j = 2; j < k; j ++){
tmp[j-1] = tmp[j];
}
k--;
sort(tmp, tmp+k);
}
return n;
}
char str[1001];
int main()
{
freopen("in1.txt","r",stdin);
int len,i;
int original, now;
while(scanf("%s", str)&&strcmp(str,"END")!=0){
init();
len = strlen(str);
for(i = 0; i < len; i ++){
if(str[i]-‘A‘<26){
num[str[i]-‘A‘] ++;
}
else {
num[26] ++;
}
}
original = 8*len;
now = fun();
printf("%d %d %.1lf\n", original, now, 1.0*original/now);
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/linxingqianglai/article/details/48137327