04-Tree 6. Huffman Codes (30)

Source: Internet
Author: User

04-Tree 6. Huffman Codes (30) time limit MS
Memory Limit 65536 KB
Code length limit 8000 B
Procedures for the award of questions StandardAuthor Chen, Yue

In 1953, David A. Huffman published his paper "A Method for the construction of Minimum-redundancy Codes", and hence print Ed his name, the history of computer. As a professor who gives the final exam problem on Huffman codes, I am encountering a big problem:the Huffman codes is N OT unique. For example, given a string "Aaaxuaxz", we can observe that the frequencies of the characters ' a ', ' X ', ' u ' and ' Z ' is 4, 2, 1 and 1, respectively. We may either encode the symbols as {' A ' =0, ' x ' =10, ' u ' =110, ' z ' =111}, or ' another ' as {' A ' =1, ' x ' =01, ' u ' =001, ' z ' =0 XX}, both compress the string into the 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} was not Correc t since "Aaaxuaxz" and "Aazuaxax" can both is decoded from the code 00001011001001. The students is submitting all kinds of codes, and I need a computer program to help me determine which ones is correct and which ones is not.

Input Specification:

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

C[1] f[1] c[2] f[2] ... c[n]

Where C[i] is a character chosen from {' 0 '-' 9 ', ' a '-' Z ', ' a '-' Z ', ' _ '}, and f[i] are the frequency of c[i] and is an Integer no more than 1000. The next line gives a positive integer m (<=1000) and 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 a string of ' 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.

Sample Input:
7A 1 B 1 C 1 D 3 E 3 F 6 G 64A 00000B 00001C 0001D 001E 01F 10G 11A 01010B 01011C 0100D 011E 10F 11G 00A 000B 001C 010D 01 1E 100F 101G 110A 00000B 00001C 0001D 001E 00F 10G 11
Sample Output:
Yesyesnono
#include <stdio.h> #include <stdlib.h> #include <string.h>void percolatedown (int *heap, int parent) {/ /will position the parent out of the element filter so that it satisfies the sequence of the heap int temp = Heap[parent];int Child = 2 * PARENT;IF (child + 1 <= heap[0] && Heap[child + 1  ] < Heap[child]) ++child;while (child <= heap[0] && Heap[child] < temp) {heap[parent] = Heap[child];p arent = Child;child = 2 * PARENT;IF (child + 1 <= heap[0] && heap[child + 1] < Heap[child]) ++child;} Heap[parent] = temp;} void buildminheap (int *heap) {//has been satisfied with the structure, the position of the adjustment element satisfies the heap order for (int i = heap[0]/2; i > 0; i)//all non-leaf nodes according to the sequence of the filter Percolatedown (he AP, i);} int deletemin (int *heap) {//deletes and returns the top element of the heap, while adjusting the structure maintenance of the heap order int minelem = heap[1];heap[1] = heap[heap[0]--];// Move the last element to heap top Percolatedown (heap, 1);//The only heap top element that does not satisfy the heap sequence is the filter return Minelem;} void Insertminheap (int *heap, int weight) {//Insert an element into the heap heap[++heap[0]] = weight;//is inserted at the end of the heap//the node in the ancestor of the insertion node is filtered in turn, Procedure is equivalent to filtering (slightly more overhead) for (int i = heap[0]/2; i > 0 && heap[i] > weight; I/= 2) PercolatedoWN (heap, i);} int calwpl (int *freq) {int heap[100] = {};//huffman tree used heap, 0-bit holds element size, 1-bit start-save weight int size = 0;for (int i = 0; i <; ++i) { Place the ownership value in the empty heap, waiting for Jian Yu (adjusted to be ordered) if (Freq[i]) {heap[++size] = Freq[i];}} Heap[0] = size;//0 position saves the number of elements in the heap buildminheap (heap);//Jian Yu//Simulation Build Huffman Tree process: each time from the heap of the lowest value of the two subtree to merge, the merged tree (the right is subtree right and) into the heap; The WPL value = The WPL value of the two subtree (the path within the subtree and the weight of the tree) + two subtree weights and (the weights are 1 paths long between parent and child nodes);//So you do not need to build Huffman tree, just save the above two values, where the weight of the subtree and stored in the heap, The WPL variable holds the WPL value of the subtree and is used to accumulate int WPL = 0;for (int i = 1; i < size; ++i) {int weight1 = deletemin (heap); int weight2 = Deletemin ( heap); WPL + = weight1 + weight2;insertminheap (heap, weight1 + weight2);} return WPL;}  int Isprefix (char *s1, char *s2) {//Determines whether two strings belong to a prefix code relationship while (S1 && s2 && *s1 = = *s2)//loop to the first different letter or end position ++s1, ++s2;if (*s1 = = ' \ "| | *s2 = = ' + ')//If there is an end position at this point, the string must be the prefix of the other string return 1;elsereturn 0;} int Hasprefixcode (char s[][200], int n) {//To determine whether n strings contain a prefix code for (int i = 0; i < n; ++i) for (int j = i + 1; j < n; ++j) I F (Isprefix (S[i], s[j])//22 Make a comparison return 1;return 0;} int main () {freopen ("test.txt", "R", stdin), int n;scanf ("%d", &n), int freq[256] = {};for (int i = 0; i < n; ++i) {C Har ch;int Num;getchar (); scanf ("%c%d", &ch, &num); freq[ch] = num;} int WPL = CALWPL (freq);//Simulation Build Huffman Tree process calculation WPL (weighted path length) int k;//k test Case scanf ("%d", &k); while (k--) {char Ch[256];char s[ 256][200];int THISWPL = 0;for (int i = 0; i < n; ++i) {scanf ("\n%c%s", &ch[i], s[i]); THISWPL + = Freq[ch[i]] * Strl En (S[i]);//}if (THISWPL = = WPL &&!hasprefixcode (S, N))//According to the encoded cumulative weighted path,//satisfies the length of the weighted path and does not contain the prefix code, then satisfies the Huffman encoding printf (" Yes\n "); elseprintf (" no\n ");} return 0;}


Title Link: http://www.patest.cn/contests/mooc-ds/04-%E6%A0%916

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

04-Tree 6. Huffman Codes (30)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.