字符编码(哈夫曼编码)

Question

请设计一个算法,给一个字符串进行二进制编码,使得编码后字符串的长度最短。

Algorithm

哈夫曼编码,权为各个字符出现的频率,再借助小根堆计算。
result=词频1*深度1+词频2*深度2…
词频可以借助哈希表统计,而深度可以通过小根堆来实现
比如:
MT-TECH-TEAM
哈希表统计完字符出现的次数后,为1,1,1,2,2,2,3
找出权最小的2个(1,1),生成新的结点2(重复此步骤)
1,2,2,2,2,3(res=2)
2,2,2,3,3(res=2+3=5)
2,3,3,4(res=5+4=9)
3,4,5(res=9+5=14)
5,7(res=14+7=21)
12(res=21+12=33)

这样做的依据是可以计算每个权在树中的深度。

Code

#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<queue>
#include<map>
#include<functional>
using namespace std;

int main(){
    string str;
    while (cin >> str){
        map<char, int> hash;
        for (int i = 0; i<str.size(); i++){
            hash[str[i]]++;
        }
        map<char, int>::iterator it = hash.begin();
        priority_queue<int, vector<int>, greater<int>> q;
        int cnt = 0;
        for (; it != hash.end(); it++){
            q.push(it->second);
            cnt++;
        }
        int x1, x2;
        int res = 0;
        while (--cnt > 0){
            x1 = q.top(); q.pop();
            x2 = q.top(); q.pop();
            res += x1 + x2;
            q.push(x1 + x2);
        }
        cout << res << endl;
    }
    return 0;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值