eolymp
bolt
Try our new interface for solving problems
Problems

Huffman Trees

Huffman Trees

A relatively simple method for compressing data works by creating a so-called \textit{Huffman tree} for a file and using it to compress and decompress the data it contains. For most applications, binary Huffman trees are used (i.e., each node is either a leaf or has exactly two sub-nodes). One can, however, construct Huffman trees with an arbitrary number of sub-trees (i.e, ternary or, in general, \textbf{N}-ary trees). A Huffman tree for a file containing \textbf{Z} different characters has \textbf{Z} leaves. The path from the root to a leaf that represents a certain character determines its encoding; each step towards the leaf determines an encoding character (which can be \textbf{0}, \textbf{1}, ..., (\textbf{N-1})). By placing often-occurring characters closer to the root, and less often occurring characters further away from the root, the desirable compression is achieved. Strictly speaking, such a tree is a Huffman tree only if the resulting encoding takes the minimal number of N-ary symbols to encode the complete file. For this problem, we only consider trees where each node is either an internal node, or a leaf encoding a character; there are no dangling leaves that do not encode for a character. The figure below shows a sample ternary Huffman tree; the characters '\textbf{a}' and '\textbf{e}' are encoded using one ternary symbol; the less frequently occurring characters '\textbf{s}' and '\textbf{p}' are encoded using two ternary symbols; and the very rare symbols '\textbf{x}', '\textbf{q}' and '\textbf{y}' are encoded using three ternary symbols each. \includegraphics{https://static.e-olymp.com/content/44/440d9c2a24e56ece64e196a6dd41ace76285106e.jpg} Of course, if we want to decompress a list of \textbf{N}-ary symbols later on, it is important to know which tree was used to compress the data. This can be done in several ways. In this problem we use the following method: the stream of data will be preceded by a header consisting of the encoded versions of the \textbf{Z} characters occurring in the original file, in lexicographical order. Given the number of source symbols \textbf{Z}, a value \textbf{N} denoting the 'arity' of the Huffman tree, and a header, give the mapping from source symbols to encoded symbols. \InputFile The input starts with a single integer \textbf{T} on a separate line, denoting the number of test cases that follow. Next, for each of the \textbf{T} test cases, three lines follow: \begin{itemize} \item The number of different characters in the file (\textbf{2} ≤ \textbf{Z} ≤ \textbf{20}); \item The number \textbf{N}, denoting the 'arity' of the Huffman tree (\textbf{2} ≤ \textbf{N} ≤ \textbf{10}); \item A string representing the header of the received message; each character will be a digit in the range \[\textbf{0}, (\textbf{N-1})\]. This string will contain less than \textbf{200} characters. \end{itemize} \textit{\textbf{Note:}} Although rare, it is possible for a header to have multiple interpretations (e.g., the header '\textbf{010011101100}' with\textbf{Z = 5} and \textbf{N = 2}). It is guaranteed that \textit{all cases in the test input have a unique solution}. \OutputFile For each of the \textbf{T} test-cases, print \textbf{Z} lines giving the encoded version of each of the \textbf{Z} characters in the testcase in ascending order. Use the format \textit{original->encoding}, where \textit{original} is a decimal value in the range \[\textbf{0}, (\textbf{Z-1})\] and encoding is the string of encoding digits for this symbol (each digit is ≥ \textbf{0} and < \textbf{N}).
Time limit 3 seconds
Memory limit 32 MiB
Input example #1
3 
3 
2 
10011 
4 
2 
000111010 
19 
10 
01234678950515253545556575859
Output example #1
0->10
1->0
2->11
0->00
1->011
2->1
3->010
0->0
1->1
2->2
3->3
4->4
5->6
6->7
7->8
8->9
9->50
10->51
11->52
12->53
13->54
14->55
15->56
16->57
17->58
18->59
Source ACM ICPC NWERC 2002