05-Tree 9 Huffman Codes (30 min)

Source: Internet
Author: User

05-Tree 9 Huffman Codes (30 min)

In 1953, David A. Huffman published his paper "A Method for the construction of Minimum-redundancy Codes", and hence PR Inted 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:

The

Each input file contains the one test case. For each case, the first line gives an integer < Span class= "Strut bottom" >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]

wherec[i]is a character chosen from {' 0 '-' 9 ', ' a '-' Z ', ' a '-' Z ', ' _ '}, andf[i]is the frequency ofc[i]And is a integer no more than 1000. The next line gives a positive integerm  ( ≤10 00), then followed By m student submissions. Each student submission consists Of n lines, each in the Format:

c[i] code[i]

The where is the-th character and is a c[i] i code[i] non-empty string of no more than 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:
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 011E 100F 101G 110A 00000B 00001C 0001D 001E 00F 10G 11
Sample Output:
YesYesNoNo


This line of data provided by the other party
A 1 B 1 C 1 D 3 E 3 F 6 G 6
It didn't work.



Ideas
Case of Example 3

In the last layer, the leaf nodes with the same encoded length (max) can only be even.
Their upper level, the number of parent nodes is the number of child nodes in this layer divided by 2. At this point, the number of parent nodes and the sum of the leaf nodes of this layer are also even.
That is, the parent node either has no child nodes, or it must have two child nodes


Case of Example 4
It is the kind of confusion that begins with WHO.



The encoded characters are double-digit, and the commit is the equal-length encoding. Error algorithm for determining leaf node and degree of a card only

This item did not pass. I don't understand what that means, either. Code changes.

Using system;using system.collections.generic;using system.threading;using system.threading.tasks;using System.diagnostics;using system.net;using system.text;using system.xml;class t{public class MyItem {public        String Value;        public string Letter;    public int valuelen;        } static void Main (string[] args) {list<myitem> List = new list<myitem> (); var count = Int.        Parse (Console.ReadLine ()); var line = Console.ReadLine ().        Split ('); for (int i = 0; i < line. Length; i++) {list.        ADD (New myitem () {letter = line[i++], Value = line[i]}); } var testcount = Int.        Parse (Console.ReadLine ());            for (int i = 0; i < Testcount; i++) {list<myitem> testlist = new list<myitem> (); for (int j = 0; J < Count; J + +) {var templine = Console.ReadLine ().                Split ('); Testlist.add (New myitem () {letter = Templine[0], Value = templine[1], Valuelen = templine[1].            Length});            } if (check (testlist)) {Console.WriteLine ("Yes");            } else {Console.WriteLine ("No");    }}}//public class node//{//public node left;    public Node right;        } private static bool Check (list<myitem> testlist) {int maxlen = 0; int minlen = Int.        MaxValue; foreach (var item in testlist) {if (maxlen< item.valuelen) {maxlen = Item            . Valuelen;            } if (Minlen>item.valuelen) {minlen = Item.valuelen;        }} int point=0;            for (int i=maxlen;i>=minlen;i--) {var items = Testlist.findall (E = = E.valuelen = i); Point + = items.            Count;            if (point%2==1) {return false;      }      else {point = POINT/2; }} if (Testlist.count < 10) {//To fool the place, this loop has a maximum n&m timeout. But the code that is removed here, the maximum n&m can be passed. This            Way trickery passed.            Testlist.sort ((a, b) = A.valuelen < B.valuelen -1:1);                for (int i = 0, i < Testlist.count; i++) {for (int j = i + 1; j < Testlist.count; J + +)                        {if (Testlist[j].valuelen >= testlist[i].valuelen) { if (Testlist[j]. Value.startswith (Testlist[i].                        Value) {return false;    }}}}} return true; } }

  

05-Tree 9 Huffman Codes (30 min)

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.