The letter with the lower frequency is assigned the code 0, and the other letter is assigned the code 1. (If each letter in a combined group has the same frequency, then 0 is assigned to the one earliest in the alphabet. For the purpose of comparisons, the value of a “combination letter” is the value of the earliest letter in the combination.) The final code sequence for a source symbol is formed by concatenating the target alphabet symbols assigned as each combination letter using the source symbol is formed.
The target symbols are concatenated in the reverse order that they are assigned so that the first symbol in the final code sequence is the last target symbol assigned to a combination letter.
The two illustrations below demonstrate the process for R = 2.
First illustration:
| Symbol | Frequency |
|---|---|
| A | 5 |
| B | 7 |
| C | 8 |
| D | 15 |
Second illustration:
| Symbol | Frequency |
|---|---|
| A | 7 |
| B | 7 |
| C | 7 |
| D | 7 |
When R is larger than 2, R symbols are grouped in each pass. Since each pass effectively replaces R letters or combination letters by 1 combination letter, and the last pass must combine R letters or combination letters, the source alphabet must contain k ∗ (R − 1) + R letters, for some integer k.
Since N may not be this large, an appropriate number of fictitious letters with zero frequencies must be included. These fictitious letters are not to be included in the output. In making comparisons, the fictitious letters are later than any of the letters in the alphabet.
Now the basic process of determining the Huffman encoding is the same as for the R = 2 case. In each pass, the R letters with the lowest frequencies are grouped, forming a new combination letter with a frequency equal to the sum of the letters included in the group. The letters that were grouped are assigned the target alphabet symbols 0 through R − 1. 0 is assigned to the letter in the combination with the lowest frequency, 1 to the next lowest frequency, and so forth. If several of the letters in the group have the same frequency, the one earliest in the alphabet is assigned the smaller target symbol, and so forth.
The illustration below demonstrates the process for R = 3.
| Symbol | Frequency |
|---|---|
| A | 5 |
| B | 7 |
| C | 8 |
| D | 15 |
The input will contain one or more data sets, one per line. Each data set consists of an integer value for R (between 2 and 10), an integer value for N (between 2 and 26), and the integer frequencies f1 through fN, each of which is between 1 and 999.
The end of data for the entire input is the number 0 for R; it is not considered to be a separate data set.
For each data set, display its number (numbering is sequential starting with 1) and the average target symbol length (rounded to two decimal places) on one line. Then display the N letters of the source alphabet and the corresponding Huffman codes, one letter and code per line. Print a blank line after each test case.
The examples below illustrate the required output format.