ZOJ1117 POJ1521 HDU1053 Huffman编码

简介: Huffman编码的思想就是贪心,我们这里使用stl里的优先队列,priority_queue使用堆进行优化,虽然自己也可以写一个堆,但我感觉对于这道题有点主次不分了,再次感觉到stl确实是一个很强大的东西。

Description


An entropy encoder is a data encoding method that achieves lossless data compression by encoding a message with "wasted" or "extra" information removed. In other words, entropy encoding removes information that was not necessary in the first place to accurately encode the message. A high degree of entropy implies a message with a great deal of wasted information; english text encoded in ASCII is an example of a message type that has very high entropy. Already compressed messages, such as JPEG graphics or ZIP archives, have very little entropy and do not benefit from further attempts at entropy encoding. …………………………


   题意是输入一字符串,然后进行Huffman编码,输出原字符串所占的长度,编码后所占的长度,以及两个长度之间的比值。


   Huffman编码的思想就是贪心,我们这里使用stl里的优先队列,priority_queue使用堆进行优化,虽然自己也可以写一个堆,但我感觉对于这道题有点主次不分了,再次感觉到stl确实是一个很强大的东西。


  解题代码


//ZOJ1117 POJ1521 HDU1053  Huffman编码
#include <stdio.h>
#include <queue>
#include <string.h>
#include <vector>
using namespace std;
char str[500];
int cnt[260];
struct cmp                                       
{
    bool operator()(int a, int b)
    {
        return a > b;
    }
};
/*我们可以直接priority_queue<int>  这样它默认的是使用vector<int> 并且是大的优先
这里我们刚好相反,用小的优先,所以要自定义优先级cmp, 这个也可以使用类来实现
class cmp
{
    public:
    bool operator() (int a, int b)
    {
        return a > b;
    }
};
*/
priority_queue <int, vector<int>, cmp> q;
int main()
{
    while (scanf("%s",str) && strcmp(str,"END"))
    {
        int l = strlen(str);
        memset(cnt, 0, sizeof(cnt));      
        //我们不需要清空队列,因为每次操作完后它都被清空了
        for (int i = 0; i < l; i++)
        {
            cnt[str[i]]++;
        }
        for (int i = 0; i < 260; i++)
        {
            if (cnt[i])
            {
                q.push(cnt[i]);
            }
        }
        int nl = 0;
        if (q.size() == 1)
            nl = l;
        while (q.size() > 1)
        {
            int a = q.top(); q.pop();
            int b = q.top(); q.pop();
            q.push(a+b);
            nl += (a+b);                               
            //我想看到这也许会有些疑惑,为什么要加(a+b),稍微思考一下就明白了
        }
        q.pop();
        printf("%d %d %.1lf\n",8*l, nl, (double)(8*l)/(double)nl);
    }
    return 0;
}
目录
相关文章
|
8月前
|
Java
hdu-1869-六度分离(dijkstra)
hdu-1869-六度分离(dijkstra)
49 0
HDU-1370,Biorhythms(中国剩余定理)
本题主要就是应用中国剩余定理。
|
算法 C++
HDOJ(HDU) 2109 Fighting for HDU(简单排序比较)
HDOJ(HDU) 2109 Fighting for HDU(简单排序比较)
121 0
ZOJ 1403&&HDU 1015 Safecracker【暴力】
Safecracker Time Limit: 2 Seconds      Memory Limit: 65536 KB === Op tech briefing, 2002/11/02 06:42 CST === "The item is locked in a Klein safe behind a painting in the second-floor library.
1224 0
|
算法 计算机视觉 存储
【HDU 2586 How far away?】LCA问题 Tarjan算法
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 题意:给出一棵n个节点的无根树,每条边有各自的权值。给出m个查询,对于每条查询返回节点u到v的最短路径的权值和,按查询顺序输出结果。
1064 0
POJ-1003-hangover
Description How far can you make a stack of cards overhang a table? If you have one card, you can create a maximum overhang of half a card length.
771 0
|
人工智能 BI JavaScript
POJ 2260(ZOJ 1949) Error Correction 一个水题
Description A boolean matrix has the parity property when each row and each column has an even sum, i.
1178 0