-->
当前位置:首页 > 题库

PROGRAMMING:Huffman coding

Luz5年前 (2021-05-10)题库716
Given a paragraph of text, if we count the frequency of letters, we can give a set of coding according to Huffman algorithm, so that we can get the shortest coding length by using this coding to compress the original text. However, Huffman coding is not the only one. For example, for the string "aaaxuaxz", it is easy to get that the occurrence frequencies of the letters' a ','x','u ','z' correspond to 4, 2, 1 and 1. We can design codes {'a' = 0, 'x' = 10, 'U' = 110, 'Z' = 111}, we can also use another set of codes {'a' = 1, 'x' = 01, 'U' = 001, 'Z' = 000}, we can also use {'a' = 0, 'x' = 11, 'U' = 100, 'Z' = 101} to compress the original text to 14 bytes. But {'a' = 0, 'x' = 01, 'U' = 011, 'Z' = 001} is not Huffman coding, because after this set of coding is compressed to 00001001001, the decoding result is not unique, "aaaxuaxz" and "aazuaxz" can correspond to the decoding result. Please judge whether any set of codes is Huffman code.
###Input format:
First, the first line gives a positive integer $$n $($$2 / Le n / Le 63 $$), and then the second line gives $$n $$non repeating characters and their frequency of occurrence. The format is as follows:
```
c[1] f[1] c[2] f[2] ... c[N] f[N]
```
Where 'C [i]' is the set {0 '-'9','a '-'z','a '-'z', ''} Characters in` F [i] 'is the frequency of occurrence of' C [i] ', an integer not exceeding 1000. The next line gives a positive integer $$M $$($$Le 1000 $$), followed by the encoding of the $$M $$set to be checked. Each set of codes takes up $$n $$lines, and the format is:
```
c[i] code[i]
```
Where 'C [i]' is the 'I' th character` Code [i] 'is a non empty string with no more than 63' 0 'and' 1 '.
###Output format:
For each set of code to be inspected, if it is the correct Huffman code, output "yes" in one line, otherwise output "no".
Note: the optimal coding is not necessarily obtained by Huffman algorithm. Any prefix code that can be compressed to the optimal length should be judged as correct.
###Input example:
```in
seven
A 1 B 1 C 1 D 3 E 3 F 6 G 6
four
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
```
###Output example:
```out
Yes
Yes
No
No
```







answer:If there is no answer, please comment