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), 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 (≤), 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.haffman算法建立最优二叉树,递归算得最短编码数。
1.1可以利用递归求得
1.2也可以再建最优二叉树的同时累加非叶结点。
2.前缀码+最短编码->正确
关键函数:
bool checkSeq(int Min, int N) { char c; char code[MAXCHNUM][MAXCHNUM]; int Codelen = 0; for(int i=1; i<=N; i++) { getchar(); scanf("%c %s", &c, code[i]); if(strlen(code[i]) > N-1) return false; Codelen += strlen(code[i]) * freq[i]; } if(Codelen != Min) return false; CheckTree root = new ChNode; CheckTree tmp; for(int i=1; i<=N; i++) { tmp = root; int stlen = strlen(code[i]); for(int j=0; j<stlen; j++) { if(code[i][j] == '0') { if(tmp->Left == NULL) tmp->Left = new ChNode; else { if(tmp->Left->flag == 1) return false; } tmp = tmp->Left; } else { if(code[i][j] == '1') { if(!tmp->Right) tmp->Right = new ChNode; else { if(tmp->Right->flag == 1) return false; } tmp= tmp->Right; } } } tmp->flag = 1; if(tmp->Left || tmp->Right) return false; } return true; }
参考代码:
#include <cstdio> #include <cstring> #include <stdlib.h> #define MINDATA -1 #define MAXCHNUM 66 int freq[MAXCHNUM]; typedef struct HTNode * HuffmanTree; struct HTNode { int Weight; HuffmanTree Left = NULL; HuffmanTree Right = NULL; }; typedef struct HTNode * ElementType; typedef struct HNode *MinHeap; struct HNode { ElementType Data[MAXCHNUM]; int Size; int Capacity; }; MinHeap CreateHeap( int MaxSize ) { MinHeap H = new HNode; H->Size = 0; H->Capacity = MaxSize; ElementType T = new HTNode; T->Weight = -1; H->Data[0] = T; return H; } void ReadData(int Num, MinHeap H); void PercDown( MinHeap H, int p ); void BuildHeap( MinHeap H ); ElementType DeleteMin( MinHeap H ); HuffmanTree Huffman(MinHeap H); int wpl(HuffmanTree T, int Depth); void insert(MinHeap H, HuffmanTree T); bool checkSeq(int Min, int N); typedef struct ChNode * CheckTree; struct ChNode { int flag = 0; CheckTree Left = NULL; CheckTree Right = NULL; }; bool checkSeq(int Min, int N) { char c; char code[MAXCHNUM][MAXCHNUM]; int Codelen = 0; for(int i=1; i<=N; i++) { getchar(); scanf("%c %s", &c, code[i]); if(strlen(code[i]) > N-1) return false; Codelen += strlen(code[i]) * freq[i]; } if(Codelen != Min) return false; CheckTree root = new ChNode; CheckTree tmp; for(int i=1; i<=N; i++) { tmp = root; int stlen = strlen(code[i]); for(int j=0; j<stlen; j++) { if(code[i][j] == '0') { if(tmp->Left == NULL) tmp->Left = new ChNode; else { if(tmp->Left->flag == 1) return false; } tmp = tmp->Left; } else { if(code[i][j] == '1') { if(!tmp->Right) tmp->Right = new ChNode; else { if(tmp->Right->flag == 1) return false; } tmp= tmp->Right; } } } tmp->flag = 1; if(tmp->Left || tmp->Right) return false; } return true; } int main() { int N; scanf("%d", &N); MinHeap H = CreateHeap(N); ReadData(N, H); HuffmanTree T = Huffman(H); int CodeLen = wpl(T, 0); int cTimes; scanf("%d", &cTimes); for(int i=0; i<cTimes; i++) { if( checkSeq(CodeLen, N) ) printf("Yes\n"); else printf("No\n"); } return 0; } int wpl(HuffmanTree T, int Depth) { if(!T->Left && !T->Right) { return Depth * T->Weight; } else { return wpl(T->Left, Depth + 1) + wpl(T->Right, Depth + 1); } } void ReadData(int Num, MinHeap H) { char c; HuffmanTree tmp; for(int i=1; i<=Num; i++) { getchar(); scanf("%c %d", &c, &freq[i]); tmp = new HTNode; tmp->Weight = freq[i]; tmp->Left = NULL; tmp->Right = NULL; H->Data[i] = tmp; H->Size++; } } void Insert( MinHeap H, HuffmanTree X ) { int i; i = ++H->Size; for ( ; H->Data[i/2]->Weight > X->Weight; i/=2 ) H->Data[i] = H->Data[i/2]; H->Data[i] = X; } HuffmanTree Huffman(MinHeap H) { int i, N; HuffmanTree T; BuildHeap( H ); N = H->Size; for(i=1; i<N; i++) { T = new HTNode; T->Left = DeleteMin(H); T->Right =DeleteMin(H); T->Weight = T->Left->Weight + T->Right->Weight; Insert(H, T); } return DeleteMin(H); } void PercDown( MinHeap H, int p ) { int Parent, Child; ElementType X; X = H->Data[p]; for( Parent=p; Parent*2<=H->Size; Parent=Child ) { Child = Parent * 2; if( (Child!=H->Size) && (H->Data[Child]->Weight > H->Data[Child+1]->Weight) ) Child++; if( X->Weight <= H->Data[Child]->Weight ) break; else /* 艙卢卢脌X */ H->Data[Parent] = H->Data[Child]; } H->Data[Parent] = X; } void BuildHeap( MinHeap H ) { int i; for( i = H->Size/2; i>0; i-- ) PercDown( H, i ); } ElementType DeleteMin( MinHeap H ) { int Parent, Child; ElementType MinItem, X; MinItem = H->Data[1]; X = H->Data[H->Size--]; for( Parent=1; Parent*2<=H->Size; Parent=Child ) { Child = Parent * 2; if( (Child!=H->Size) && (H->Data[Child]->Weight > H->Data[Child+1]->Weight) ) Child++; if( X->Weight <= H->Data[Child]->Weight ) break; else H->Data[Parent] = H->Data[Child]; } H->Data[Parent] = X; return MinItem; }