哈夫曼树 --- 优先队列(C++STL)

来源:互联网 发布:Linux mv移动文件 编辑:程序博客网 时间:2024/05/22 07:45

数据结构实验之二叉树六:哈夫曼编码
Time Limit: 1000MS Memory Limit: 65536KB
Submit Statistic
Problem Description

字符的编码方式有多种,除了大家熟悉的ASCII编码,哈夫曼编码(Huffman Coding)也是一种编码方式,它是可变字长编码。该方法完全依据字符出现概率来构造出平均长度最短的编码,称之为最优编码。哈夫曼编码常被用于数据文件压缩中,其压缩率通常在20%~90%之间。你的任务是对从键盘输入的一个字符串求出它的ASCII编码长度和哈夫曼编码长度的比值。
Input

输入数据有多组,每组数据一行,表示要编码的字符串。
Output

对应字符的ASCII编码长度la,huffman编码长度lh和la/lh的值(保留一位小数),数据之间以空格间隔。
Example Input

AAAAABCD
THE_CAT_IN_THE_HAT
Example Output

64 13 4.9
144 51 2.8
Hint

Author

xam

哈夫曼树:最优二叉树(带权路径长度最短的二叉树)。
通常规定,左子树根节点的权值<=右子树根节点的权值。

#include<bits/stdc++.h>using namespace std;int main(){    string a;    while(cin>>a)    {        int v[1210];    priority_queue<int, vector<int>, greater<int> > q;//优先队列    /*最先出队的为最大值或者最小值,可根据情况自己更改*/    memset(v,0,sizeof(v));        int la = 8 *a.size();//8位二进制        for(int i=0;i<a.length();i++)        {            v[a[i]]++;//计算个数        }        for(int i=0;i<200;i++)        {            if(v[i])              {                  q.push(v[i]);//入队              }        }        int lh = 0;        while(!q.empty())        {            int n = q.top();//出队            q.pop();//从队列中删除            if(!q.empty())            {                int m = q.top();//出队                q.pop();//删除                int s = m+n;                q.push(s);//入队                lh += s;            }        }        printf("%d %d %.1lf\n", la, lh, la*1.0/lh);    }    return 0;}

树-堆结构练习——合并果子之哈夫曼树
Time Limit: 1000MS Memory Limit: 65536KB
Submit Statistic
Problem Description

在一个果园里,多多已经将所有的果子打了下来,而且按果子的不同种类分成了不同的堆。多多决定把所有的果子合成一堆。
每一次合并,多多可以把两堆果子合并到一起,消耗的体力等于两堆果子的重量之和。可以看出,所有的果子经过n-1次合并之后,就只剩下一堆了。多多在合并果子时总共消耗的体力等于每次合并所消耗体力之和。
因为还要花大力气把这些果子搬回家,所以多多在合并果子时要尽可能地节省体力。假定每个果子重量都为1,并且已知果子的种类数和每种果子的数目,你的任务是设计出合并的次序方案,使多多耗费的体力最少,并输出这个最小的体力耗费值。
例如有3种果子,数目依次为1,2,9。可以先将1、2堆合并,新堆数目为3,耗费体力为3。接着,将新堆与原先的第三堆合并,又得到新的堆,数目为12,耗费体力为12。所以多多总共耗费体力=3+12=15。可以证明15为最小的体力耗费值。

Input

第一行是一个整数n(1<=n<=10000),表示果子的种类数。第二行包含n个整数,用空格分隔,第i个ai(1<=ai<=20000)是第i个果子的数目。

Output

输出包括一行,这一行只包含一个整数,也就是最小的体力耗费值。输入数据保证这个值小于2^31。

Example Input

3
1 2 9
Example Output

15
Hint

Author

赵利强

think:和上一道题几乎一样,不过这道题比较简单。

#include<bits/stdc++.h>using namespace std;int main(){    int n, m;    scanf("%d", &n);    priority_queue<int, vector<int>, greater<int> > q;        for(int i=0;i<n;i++)        {           scanf("%d", &m);            q.push(m);        }        int sum = 0;        while(!q.empty())        {            int n = q.top();            q.pop();            if(!q.empty())            {                int m = q.top();                q.pop();                int s = m+n;                q.push(s);                sum += s;            }        }        printf("%d\n", sum);    return 0;}

下面这道题目,特别的坑,在看到了一片WA之后,才AC了……坑点在于,多组输入,并且,数据范围要足够的大,long long int,否则过不了……
Fence Repair
Time Limit: 2000MS Memory Limit: 65536KB
Submit Statistic Discuss
Problem Description

Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000) planks of wood, each having some integer length Li (1 ≤ Li ≤ 50,000) units. He then purchases a single long board just long enough to saw into the N planks (i.e., whose length is the sum of the lengths Li). FJ is ignoring the “kerf”, the extra length lost to sawdust when a sawcut is made; you should ignore it, too.
FJ sadly realizes that he doesn\’t own a saw with which to cut the wood, so he mosies over to Farmer Don\’s Farm with this long board and politely asks if he may borrow a saw.
Farmer Don, a closet capitalist, doesn\’t lend FJ a saw but instead offers to charge Farmer John for each of the N-1 cuts in the plank. The charge to cut a piece of wood is exactly equal to its length. Cutting a plank of length 21 costs 21 cents.
Farmer Don then lets Farmer John decide the order and locations to cut the plank. Help Farmer John determine the minimum amount of money he can spend to create the N planks. FJ knows that he can cut the board in various different orders which will result in different charges since the resulting intermediate planks are of different lengths.

Input

Line 1: One integer N, the number of planks
Lines 2..N+1: Each line contains a single integer describing the length of a needed plank

Output

Line 1: One integer: the minimum amount of money he must spend to make N-1 cuts

Example Input

3
8
5
8
Example Output

34
Hint

Author

#include <bits/stdc++.h>using namespace std;int main(){   long long  int n;    long long int m;    priority_queue <long long int, vector<long long int>, greater<long long int> > q;    while(~scanf("%lld", &n))    {    for(int i=0;i<n;i++)    {        scanf("%d", &m);       q.push(m);    }    m = 0;    while(!q.empty())    {        long long int a = q.top();        q.pop();        if(!q.empty())        {            long long int b = q.top();            q.pop();            long long int c = a + b;            q.push(c);            m += c;        }    }      printf("%lld\n", m);    }    return 0;}
原创粉丝点击