05-树9 Huffman Codes(30 分)
In 1953, David A. Huffman published his paper “A Method for the Construction of Minimum-Redundancy Codes”, and hence printed his name in the history of computer science. As a professor who gives the final exam problem on Huffman codes, I am encountering a big problem: the Huffman codes are NOT unique. For example, given a string “aaaxuaxz”, we can observe that the frequencies of the characters ‘a’, ‘x’, ‘u’ and ‘z’ are 4, 2, 1 and 1, respectively. We may either encode the symbols as {‘a’=0, ‘x’=10, ‘u’=110, ‘z’=111}, or in another way as {‘a’=1, ‘x’=01, ‘u’=001, ‘z’=000}, both compress the string into 14 bits. Another set of code can be given as {‘a’=0, ‘x’=11, ‘u’=100, ‘z’=101}, but {‘a’=0, ‘x’=01, ‘u’=011, ‘z’=001} is NOT correct since “aaaxuaxz” and “aazuaxax” can both be decoded from the code 00001011001001. The students are submitting all kinds of codes, and I need a computer program to help me determine which ones are correct and which ones are not.

Input Specification:

Each input file contains one test case. For each case, the first line gives an integer N (2≤N≤63), then followed by a line that contains all the N distinct characters and their frequencies in the following format:

c[1] f[1] c[2] f[2] … c[N] f[N]
where c[i] is a character chosen from {‘0’ - ‘9’, ‘a’ - ‘z’, ‘A’ - ‘Z’, ‘_’}, and f[i] is the frequency of c[i] and is an integer no more than 1000. The next line gives a positive integer M (≤1000), then followed by M student submissions. Each student submission consists of N lines, each in the format:

c[i] code[i]
where c[i] is the i-th character and code[i] is an non-empty string of no more than 63 ‘0’s and ‘1’s.

Output Specification:

For each test case, print in each line either “Yes” if the student’s submission is correct, or “No” if not.

Note: The optimal solution is not necessarily generated by Huffman algorithm. Any prefix code with code length being optimal is considered correct.

Sample Input:

7
A 1 B 1 C 1 D 3 E 3 F 6 G 6
4
A 00000
B 00001
C 0001
D 001
E 01
F 10
G 11
A 01010
B 01011
C 0100
D 011
E 10
F 11
G 00
A 000
B 001
C 010
D 011
E 100
F 101
G 110
A 00000
B 00001
C 0001
D 001
E 00
F 10
G 11
Sample Output:

Yes
Yes
No
No

#include <iostream>
#include <string>
using namespace std;
const int minData=-1;
struct HuffmanTree
{int f;HuffmanTree *left=NULL;HuffmanTree *right=NULL;
};
struct minHeap
{HuffmanTree *data;int Size=0;
};
minHeap* Create(int maxSize)
{minHeap* H=new minHeap;H->data=new HuffmanTree[maxSize+1];H->data[0].f=minData;return H;
}
void Insert(HuffmanTree* x,minHeap* H)
{int i=++H->Size;for(;H->data[i/2].f>x->f;i/=2){H->data[i]=H->data[i/2];}H->data[i]=*x;
}
HuffmanTree* Delete(minHeap *H)
{int parent,child;HuffmanTree temp=H->data[H->Size--];HuffmanTree *minItem=new HuffmanTree;*minItem=H->data[1];for(parent=1;parent*2<=H->Size;parent=child){child=parent*2;if(child+1<=H->Size&& H->data[child].f>H->data[child+1].f){child++;}if(temp.f<=H->data[child].f) break;else H->data[parent]=H->data[child];}H->data[parent]=temp;return minItem;
}
HuffmanTree* BuildHuffmanTree(int m,minHeap* H)
{HuffmanTree* T;for(int i=0;i<m-1;i++){T=new HuffmanTree;T->left=Delete(H);T->right=Delete(H);T->f=T->left->f+T->right->f;Insert(T,H);}return Delete(H);
}
int wpl(HuffmanTree* T,int h)
{if(!T->left && !T->right) return h*(T->f);else return wpl(T->left,h+1) + wpl(T->right,h+1);
}
void check(int n,int freq[],HuffmanTree* T)
{int i,j,is=1,sum=0;char c;string s[n];for(i=0;i<n;i++){cin>>c>>s[i];if(s[i].size()>n-1) break;for(j=0;j<i;j++){if(s[i]==s[j]) break;if(s[i].size()+1==s[j].size()){if(s[i]==s[j].substr(0,s[i].size())) break;}}if(j<i) break;sum+=s[i].size()*freq[i];}if(i<n || sum!=wpl(T,0)) is=0;for(;i<n-1;i++) cin>>c>>s[i];if(is) cout<<"Yes"<<endl;else cout<<"No"<<endl;
}
int main()
{int m,n;char c;cin>>m;int freq[m];minHeap* H=Create(m);HuffmanTree* T;for(int i=0;i<m;i++){cin>>c>>freq[i];HuffmanTree* node=new HuffmanTree;node->f=freq[i];Insert(node,H);}T=BuildHuffmanTree(m,H);cin>>n;for(int i=0;i<n;i++){check(m,freq,T);}return 0;
}

转载于:https://www.cnblogs.com/JingwangLi/p/10202823.html

05-树9 Huffman Codes相关推荐

  1. 数据结构实验三:Huffman树及Huffman编码的算法实现

    Exp03 Huffman树及Huffman编码的算法实现 Author: Maskros 实验目的 了解该树的应用实例,熟悉掌握Huffman树的构造方法及Huffman编码的应用, 了解Huffm ...

  2. Huffman Codes

    题目 In 1953, David A. Huffman published his paper "A Method for the Construction of Minimum-Redu ...

  3. (C语言详解)05-树9 Huffman Codes(详细解析)

    05-树9 Huffman Codes (30分) In 1953, David A. Huffman published his paper "A Method for the Const ...

  4. PTA 05-树9 Huffman Codes

    PTA 05-树9 Huffman Codes 原题链接 题目理解 [来自陈越姥姥的讲解视频和PPT+自己的记录] (1)Huffman编码不唯一 0和1可能互换 树的形状可能不一样 但是都是最优Hu ...

  5. Pat 04-树6. Huffman Codes (30)

    题目链接: Huffman codes 题意: 先给出N个节点的出现次数 再给出M种编码方式 判断每种编码方式是否能构成哈夫曼树 题解: 判断哈夫曼编码的条件有两个: 1  哈夫曼编码不唯一,但它的W ...

  6. huffman树和huffman编码

    不知道为什么,我写的代码都是又臭又长. 直接上代码: #include <iostream> #include <cstdarg> using namespace std; c ...

  7. 05-树9 Huffman Codes (30 分)

    05-树9 Huffman Codes (30 分) In 1953, David A. Huffman published his paper "A Method for the Cons ...

  8. 离散数学与组合数学-05树

    文章目录 离散数学与组合数学-05树 5.1 认识树 5.1.1 树的模型 5.1.2 树的应用 5.2 无向树 5.2.1 定义 5.2.2 树的性质 5.2.3 性质应用 5.3 生成树 5.3. ...

  9. Huffman树与Huffman编码

    Huffman树与Huffman编码 问题描述 已知某系统在通信联络中只可能出现6种字符,其使用频度如下表所示: 根据Huffman编码原理,为6种字符设计一种Huffman编码方案. 算法分析与设计 ...

最新文章

  1. DB_Links创建际删除
  2. Spring Boot由jar包转成war包
  3. [Boost基础]并发编程——asio网络库——定时器deadline_timer
  4. 119.CSMA/CD
  5. mongodb命令基础知识点
  6. 介绍一个能避免 CORS 错误的 Chrome 扩展 - Moesif Origin CORS Changer
  7. 安卓平板排行榜_shopee虾皮台湾安卓市场, shopee虾皮直播下载
  8. 吐血整理:C#顺序、选择、循环结构用法与案例,这一篇就够了!
  9. 信息学奥赛一本通 1168:大整数加法 | OpenJudge NOI 1.6 10:大整数加法
  10. 通过百度获取IP地址对应的经纬度
  11. STM8单片机ADC单次采样模式
  12. oracle往游标中存数据,Oracle数据库:ORACLE11G在存储过程里面遍历游标
  13. 逆置单链表c语言程序,逆置单链表C语言
  14. UVA 10791 Minimum Sum LCM 数论
  15. 数据库-创建数据库-创建数据表
  16. PHP 第三方调用 UC_Center用户登录认证
  17. 现在的媒体时兴“毁人不倦”?
  18. Spring Security认证_内存认证
  19. ToggleButton的用法
  20. android 动态改变button样式,Android 修改button颜色

热门文章

  1. 对于(不是特别不合理)的指摘、的对应方式(学会调整,不要一根筋)
  2. SQL的「悲观锁定」与「乐观锁定」
  3. 堡垒机原生ssh登陆解决方案
  4. 关于echart 图表自适应问题的解决办法
  5. JS组件系列——BootstrapTable+KnockoutJS实现增删改查解决方案(三):两个Viewmodel搞定增删改查
  6. css3 动画与display:none冲突的解决方案
  7. Nginx gzip参数详解及常见问题(已解决)
  8. charles抓包显示乱码解决方法
  9. 解决Linux Kettle出现闪退问题
  10. 如何在JavaScript中检查变量是否为整数?