hdu1053 哈弗曼编码

来源:互联网 发布:装饰公司网络推广方案 编辑:程序博客网 时间:2024/05/23 01:28

题目链接 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;}


0 0
原创粉丝点击