PAT Huffman Codes

Source: Internet
Author: User

In 1953, David A. Huffman published his paper "A Method for the construction of Minimum-redundancy Codes", and hence print Ed 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 is not unique.  for example, given a string "Aaaxuaxz", we can obse Rve that the frequencies of the characters ' a ', ' X ', ' u ' and ' Z ' is 4, 2, 1 and 1, respectively.  We may either Enco De the symbols as {' A ' =0, ' x ' =10, ' u ' =110, ' z ' =111}, or in another ' as {' A ' =1, ' x ' =01, ' u ' =001, ' z ' =000}, both compress The string into bits.  another set of code can is 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 was 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

I did not construct the Huffman tree out of this problem. My idea is that you just have to figure out Huffman's WPL and see if each node is a leaf node.
#include <stdio.h> #include <string.h> #include <stdlib.h>typedef struct ele{int freq;char ch;int Lchild;int Rchild;} Ele;void Createque (ELE *q, int n); void Percolatdown (ELE *q, int n, int i); ELE deletemin (ELE *q, int *n), void Insert (ELE *q, ELE X, int *n); int main (void) {int n, M, ntmp; ELE queue[64] = {0}; ELE EleTmp1, ELETMP2, Eletmp3;char chstr[6] = {0}, Ch;int I, J, K, T, Chstrlen = 0;//chstrlen: Calculates the length of the check string. For example A 00000, then Chstrlen is 5. int WPL, pow, SHIT;//WPL: calculated from the input queue Wpl;pow calculates the wpl;//shit of the data to be checked to save the node frequency int Dafeiji = 0;//each check to see if the node is a leaf node. 0 means none, 1 is the path of a leaf node, 2 means that the leaf node int fuck[128];//Constructs a full two fork tree, used to query whether the node is//in the path of a node//all character nodes are leaf points, need 2^k nodes//where k is the number of layers, 2^ (k-1) >= N, K takes the minimum struct {char ch;int freq;} rilegou[63];//the input of characters and frequencies, uses WPL ("%d", scanf) when calculating &n, GetChar (); for (i = 1; I &l T;= N; i++) {scanf ("%c", & (queue[i].ch)), rilegou[i-1].ch = Queue[i].ch;getchar (); scanf ("%d", & (Queue[i].freq)); Rilegou[i-1].freq = Queue[i].freq;getchar ();} Ntmp = N; Createque (queue, N); WPL = 0;while (NTMP > 1)//calculate WPL, do not construct Huffman tree, use the queue to simulate. {ELETMP1 = deletemin (queue, &ntmp); ELETMP2 = deletemin (queue, &ntmp); Eletmp3.freq = Eletmp1.freq + eletmp2.freq; eletmp3.ch =-1; WPL + = Eletmp3.freq;insert (queue, EleTmp3, &ntmp);}  scanf ("%d", &m); GetChar (); for (i = 0; i < M; i++) {memset (fuck, 0, sizeof (fuck));d Afeiji = 0;pow = 0;shit = 0;for (j = 0; J < N; J + +)//Check if it is a leaf node. The following for loop values are strlen-1. Because you want the last one to check if it is a {//leaf node. It should be possible to implement it in other ways. scanf ("%c%s", &ch, Chstr); while (GetChar ()! = ' \ n '); if (Dafeiji! = 1) {Chstrlen = strlen (CHSTR); if (chstr[chstrlen-1] = = ' \ n ') {Chstr[chstrlen] = 0;chstrlen--;} for (k = 0; k < N; k++) if (rilegou[k].ch = = ch) {shit = Rilegou[k].freq;break;} POW + = Chstrlen * Shit;t = 1;for (k = 0; k < chstrlen-1; k++) {if (chstr[k] = = ' 0 ') t *= 2;elset = t * 2 + 1;if (Fuck[t] = = 2) {Dafeiji = 1;break;} Else{fuck[t] = 1;}} if (chstr[k] = = ' 0 ') t *= 2;elset = t * 2 + 1;if (fuck[t]! = 0) Dafeiji = 1;elsefuck[t] = 2;}} if (Pow = = WPL && Dafeiji = = 0) printf ("yes\n"); elseprintF ("no\n");} return 0;} void Createque (ELE *q, int n) {int i = 0;for (i = N/2; i > 0; i--) {Percolatdown (q, N, i);}} void Percolatdown (ELE *q, int n, int i) {int J = 0;int tmp = 0; ELE eletmp;for (j = i; J * 2 <= N; j = tmp) {tmp = J * 2;IF (n! = tmp && q[tmp].freq > q[tmp + 1].freq) {tmp++ ;} if (Q[j].freq > Q[tmp].freq) {eletmp = Q[j];q[j] = q[tmp];q[tmp] = eletmp;} Else{break;}}} ELE deletemin (ELE *q, int *n) {ELE res = {0};int ntmp = *n;res = q[1];q[1] = q[ntmp]; ntmp--; Percolatdown (Q, ntmp, 1); *n = Ntmp;return res;} void Insert (ELE *q, ELE X, int *n) {int ntmp = *n;int i = 0;for (i = *n + 1; i > 1; i = I/2) {if (Q[i/2].freq >= X . freq) {Q[i] = Q[I/2];} Else{break;}} Q[i] = X; (*n) + +;}

PAT Huffman Codes

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.