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 <stdio.h>
#include <stdlib.h>
#define MinData 0
typedef struct TreeNode* HuffmanTree;
struct TreeNode
{
int weight;
HuffmanTree Left;
HuffmanTree Right;
};
typedef struct HeapStruct *MinHeap;
struct HeapStruct
{
HuffmanTree elements;
int size;
int capacity;
};
MinHeap MinHeap_Build(int weight[], int Maxsize);
MinHeap MinHeap_Creat(int Maxsize);
void MinHeap_Insert(MinHeap H, HuffmanTree HT);
HuffmanTree MinHeap_Delete(MinHeap H);
HuffmanTree HuffmanTree_Build(MinHeap H);
void Get_wpl(HuffmanTree HT, int *wpl, int layer);
int code_length(char *a);
int compare(char *c1, char *c2);
int main()
{
int N;
scanf("%d\n", &N);
char c[N];
int f[N];
for (int i = 0; i < N; ++i)
{
if(i == N-1)
scanf("%c %d", &c[i], &f[i]);
else
scanf("%c %d ", &c[i], &f[i]);
}
MinHeap MH = MinHeap_Build(f, N);
HuffmanTree HT = HuffmanTree_Build(MH);
int MinWpl = 0;
Get_wpl(HT, &MinWpl, 0);//计算最优编码长度
int M;
scanf("%d\n", &M);
char ch[N], code[N][64];
for (int j = 0; j < M; ++j)
{
for (int i = 0; i < N; ++i)
scanf("%c %s\n", &ch[i], code[i]);
int flag = 1;
for (int i = 0; i < N; ++i)//检查是否满足前缀码要求
{
for (int k = i+1; k < N; ++k)
{
if(compare(code[i], code[k]))
{
if(flag)
printf("No\n");
flag = 0;
}
}
}
int stu_wpl = 0;
if(flag)
{
for (int i = 0; i < N; ++i)
stu_wpl += f[i]*code_length(code[i]);//每组输入数据的编码长度
if(MinWpl == stu_wpl)
printf("Yes\n");
else
printf("No\n");
}
}
return 0;
}
int code_length(char *a)
{
int len = 0;
char *p = a;
while(*p != '\0')
{
p++;
len++;
}
return len;
}
int compare(char *c1, char *c2)
{
char *a = c1, *b = c2;
while(*a!='\0' && *b!='\0')
{
if(*a != *b)
return 0;
a++;
b++;
}
return 1;
}
MinHeap MinHeap_Build(int weight[], int Maxsize)
{
MinHeap H = MinHeap_Creat(Maxsize);
HuffmanTree Temp = (HuffmanTree)malloc(sizeof(struct TreeNode));
for (int i = 0; i < Maxsize; ++i)
{
Temp->weight = weight[i];
Temp->Left = NULL;
Temp->Right = NULL;
MinHeap_Insert(H, Temp);
}
free(Temp);
return H;
}
MinHeap MinHeap_Creat(int MaxSize)
{
MinHeap H = (MinHeap)malloc(sizeof(struct HeapStruct));
H->elements = (HuffmanTree)malloc(sizeof(struct TreeNode)*(MaxSize + 1));
//因为elemens[0]作为哨兵,从[1]开始存放,所以分配MaxSize+1空间
H->size = 0;
H->capacity = MaxSize;
H->elements[0].weight = MinData;//将elements[0]作为哨兵
return H;
}
void MinHeap_Insert(MinHeap H, HuffmanTree HT)
{
if(H->size == H->capacity)
return;
int i;
i = ++H->size;//i指向插入后堆中的最后一个元素的位置
for(; H->elements[i/2].weight > HT->weight; i/=2) //比较插入的结点和其父结点的大小
H->elements[i].weight = H->elements[i/2].weight;
H->elements[i] = *HT;
}
HuffmanTree MinHeap_Delete(MinHeap H)
{
HuffmanTree Temp, MinNode;
if(H->size == 0)
return NULL;
MinNode = (HuffmanTree)malloc(sizeof(struct TreeNode));
Temp = (HuffmanTree)malloc(sizeof(struct TreeNode));
*MinNode = H->elements[1];//取出根结点的最小值
*Temp = H->elements[H->size--];//用最小堆的最后一个元素从根结点开始向上过滤下层结点
int Parent, Child;
for(Parent = 1; Parent*2 <= H->size; Parent = Child)
{
Child = Parent*2;
if(Child != H->size && (H->elements[Child].weight > H->elements[Child+1].weight))
Child++;//当存在右子结点,且右子节点小于左子节点时,Child指向较小者
if(Temp->weight > H->elements[Child].weight)
H->elements[Parent] = H->elements[Child];//移动Temp到下一层
else
break;
}
H->elements[Parent] = *Temp;
free(Temp);
return MinNode;
}
HuffmanTree HuffmanTree_Build(MinHeap H)
{
HuffmanTree HT;
int times = H->size;//H->size的值会发生变化,所以要用另一个变量来存储
for (int i = 1; i < times; ++i)
{//执行初始 H->size-1 次合并
HT = (HuffmanTree)malloc(sizeof(struct TreeNode));
HT->Left = MinHeap_Delete(H);
HT->Right = MinHeap_Delete(H);
HT->weight = HT->Left->weight + HT->Right->weight;
MinHeap_Insert(H, HT);
}
HT = MinHeap_Delete(H);
return HT;
}
void Get_wpl(HuffmanTree HT, int *wpl, int layer)
{
if(HT->Left == NULL && HT->Right == NULL)
*wpl += layer * HT->weight;
else
{
Get_wpl(HT->Left, wpl, layer+1);
Get_wpl(HT->Right, wpl, layer+1);
}
}