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
这题我当时做的时候,思路是对的,就是构造出一个哈夫曼树,算出最优路径长度,然后和后面输入数据的路径长度作比较,另外检验是否是前缀码,就可以得到答案。
这题的难度在于怎么构造哈夫曼树,但是我参照了另外一个大佬的代码之后,发现其实绕过这个问题,他并没有构造哈夫曼树,使得问题大大简化了(但同时,只适用于这题,如何构造哈夫曼树还是得学的,只会简单的要不得......)
他的思路是:哈夫曼树的带权路径长度 = 所有非叶子结点的权值 = 除根节点外所有结点的权值。想一想:
叶子结点的带权路径长度 - 叶子结点的父节点的带权路径长度 = 其叶子结点的权值之和(路径长度就差1)
而这个差(其叶子结点的权值之和)又包含根节点里面了(根节点等于所有结点之和),所以:
所以其叶子结点的权值之和(根节点的那部分)+叶子结点的父节点的带权路径长度 = WPL。
而父节点的父节点也可以这么推导,所以最后得到那个推论(觉得很难理解的话,画个树记一下就行了)。
那么我们不需要去构造出一个哈夫曼树,我们只要建立一个最小堆,每次取出两个最小值相加,然后将和再插入这个和,操作n-1次后就得到了n-1个非叶子结点(这又是另一个推论了,哈夫曼树只有N个叶子结点和N-1个度为2的非叶子结点)。将这些和累加就是WPL了。
另外关于前缀码的判断,只要判断一编码字符串有没有和另一个的前缀完全一样的就行。那就一个字符一个字符的对比,当一个字符串比对完成,发现一模一样的,那就不是前缀编码。
这是大佬的代码:
https://blog.youkuaiyun.com/qq_39339575/article/details/90488227
//05-树9 Huffman Codes (30 分)
#include<stdio.h>
#include<map>
#include<string.h>
#define Maxsize 65
using namespace std;
int H[Maxsize], size;void Initialize(void);
void Insert(int Data);
int Delete(void);
int compute(void);
int single_prix(char *s1, char *s2);
int str_prix(int N, char s[][Maxsize]);int main()
{
int N=0, M=0, i=0, f=0, WPL=0;
char c;
scanf("%d", &N);
Initialize(); //初始化
map <char, int> m; //这里用结构数组其实也可以
for(i=0;i<N;i++){
getchar();
scanf("%c %d", &c, &f);
m[c]=f;
Insert(f);
}
WPL=compute();
scanf("%d", &M);
while(M--){
char ch[N], s[N][Maxsize];
int thisWPL=0;
for(i=0;i<N;i++){
scanf("\n%c %s", &ch[i], s[i]);
thisWPL+=m[ch[i]]*strlen(s[i]);
}
if(thisWPL==WPL && !str_prix(N, s)) printf("Yes");
else printf("No");
if(M!=0) printf("\n");
}
return 0;
}void Initialize(void) //堆的初始化,为了之后插入的方便
{
H[0]=-1; //哨兵结点
size=0;
}void Insert(int Data) //构造最小堆
{
int i=0;
for(i=++size;Data<H[i/2];i/=2){
H[i]=H[i/2];
}
H[i]=Data;
}int Delete(void) //删除堆中最小的元素(不明白的可以看看mooc陈越老师的课)
{
int temp=H[1], i=0;
int last=H[size];
H[size--]=0;
for(i=2;i<=size;i*=2){
if(H[i]>H[i+1] && i!=size) i++;
if(H[i]<last) H[i/2]=H[i];
else break;
}
H[i/2]=last;
return temp;
}int compute(void) //计算哈夫曼树非叶子结点的权值和
{
int length=size;
int temp1, temp2, WPL=0;
for(int i=1;i<length;i++){
temp1=Delete();
temp2=Delete();
Insert(temp1+temp2);
WPL+=temp1+temp2;
}return WPL;
}int single_prix(char *s1, char *s2) //比对编码字符串
{
while(s1 && s2 && *s1==*s2){
s1++;
s2++;
}
if(*s1=='\0' || *s2=='\0') return 1;
else return 0;
}int str_prix(int N, char s[][Maxsize])
{
for(int i=0;i<N;i++){
for(int j=i+1;j<N;j++){
if(single_prix(s[i], s[j])) return 1;
}
}
return 0;
}