huffman 优先队列

先说一下优先队列的使用
首先需要两个头文件include && include
其次普通的int型默认是从小到大排列:
priority_queue == priority

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<functional>
#include<queue>
#include<algorithm>
#include<string>
#define MAXNUM 150
using namespace std;
string str;
int cnt[40];
priority_queue<int,vector<int>,greater<int>> que;
int main()
{
    freopen("1.txt", "r", stdin);
    int i, j, k, a, b, c, ans, count;
    float tmp;
    while (cin >> str)
    {
        if (str == "END")
            break;
        memset(cnt, 0, sizeof(cnt));
        while (!que.empty())
            que.pop();
        for (i = 0; i < str.length(); i++)
        {
            if (str[i] == '_')
                cnt[26]++;
            else
                cnt[str[i] - 'A']++;
        }
        ans = 0;
        count = 0;
        for (i = 0; i <= 26; i++)
        {
            if (cnt[i])
            {
                que.push(cnt[i]);
                count++;
            }
        }
        if (count == 1)
        {
            ans = que.top();
        }
        else
        {
            while (que.size() > 1)
            {
                a = que.top(); que.pop();
                b = que.top(); que.pop();
                que.push(a + b);
                ans = ans + a + b;
            }
        }
        tmp = float(str.length() * 8);
        printf("%d %d %.1f\n", str.length() * 8, ans, tmp / ans);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值