Problem B: 神秘电报密码-哈夫曼编码

Problem B: 神秘电报密码-哈夫曼编码

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 0  Solved: 0
[Submit] [Status] [Web Board] [Creator:]

Description

创建一棵Huffman树并输出其字母的Huffman编码。

Input

第一行输入T组样例,第二行输入字母的数量n,接下来n行输入n个字母的出现频率。

Output

以如下形式输出每个字母的huffman编码(按输入顺序输出) 。

特别说明:因为Huffman树不唯一,所以此处要求按结点顺序的优先级创建huffman树(即先输入的字符在频率相同的情况下优先级更高)。

Sample Input Copy

1
6
a 0.05
b 0.32
c 0.18
d 0.07
e 0.25
f 0.13

Sample Output Copy

a: 1000 b: 11 c: 00 d: 1001 e: 01 f: 101 

HINT

Please input n:
6
Please input value and weight of leaf node 1
a 0.05
Please input value and weight of leaf node 2
b 0.32
Please input value and weight of leaf node 3
c 0.18
Please input value and weight of leaf node 4
d 0.07
Please input value and weight of leaf node 5
e 0.25
Please input value and weight of leaf node 6
f 0.13


x1.weight and x2.weight in round 1 0.05 0.07 
x1.weight and x2.weight in round 2 0.12 0.13 
x1.weight and x2.weight in round 3 0.18 0.25 
x1.weight and x2.weight in round 4 0.25 0.32 
x1.weight and x2.weight in round 5 0.43 0.57 
a: Huffman code is: 1000 
b: Huffman code is: 11 
c: Huffman code is: 00 
d: Huffman code is: 1001 
e: Huffman code is: 01 
f: Huffman code is: 101