题目链接 Entropy
只需要求编码长度,不要求输出编码结果,所以不需要建树
对AAAAABCD,如图
总的编码长度=len(B)+len(C)+len(D)+5*len(A)=3+3+2+5*1=13
两种写法,都是nlogn
(1)利用sort排序
#include<stdio.h>
#include<memory.h>
#include<string.h>
#include<algorithm>
using namespace std;
char str[1005];
int num[27];
bool cmp(int a, int b){
return a > b;
}
int main()
{
//freopen("in.txt", "r", stdin);
while (scanf("%s", str) != EOF&&strcmp(str, "END")){
memset(num, 0, sizeof(num));
int len = strlen(str);
for (int i = 0; i < len; ++i){
if (str[i] == '_')num[26]++;
else num[str[i] - 'A']++;
}
sort(num, num + 27, cmp);
int n = 26;
while (!num[n])n--;
int ans = 0;
if (n == 0){ ans = num[0]; }//注意这里!!
else for (int i = n; i >= 1; --i) {
num[i - 1] += num[i];
ans += num[i - 1];
sort(num, num + i + 1, cmp);
}
printf("%d %d %.1f\n", len * 8, ans, len*8.0 / ans);
}
return 0;
}
(2)利用有限队列
#include <stdio.h>
#include <queue>
#include <string.h>
#include <memory.h>
#include <vector>
#include <functional>
using namespace std;
char str[1005];
int main()
{
//freopen("in.txt", "r", stdin);
int num[27];
while (scanf("%s", str)!=EOF && strcmp(str, "END")) {
memset(num, 0, sizeof(num));
int len = strlen(str);
for (int i = 0; i < len; ++i){
if (str[i] == '_')num[26]++;
else num[str[i] - 'A']++;
}
priority_queue<int, vector<int>, greater<int> >pq;
for (int i = 0; i < 27; ++i){
if (num[i])pq.push(num[i]);
}
int ans = 0;
while (pq.size()>1){
int x = pq.top(); pq.pop();
int y = pq.top(); pq.pop();
ans += (x + y);
pq.push(x + y);
}
if (ans == 0)ans = len;//如果只有一个元素
printf("%d %d %.1lf\n", len * 8, ans, len*8.0 / ans);
}
return 0;
}