TopCoder

User's AC Ratio

NaN% (0/0)

Submission's AC Ratio

NaN% (0/0)

Tags

Description

Let B be a string consisting of m letters. Consider each of the letters as a symbol with a probability of occurrences. The probability of a symbol is its frequency of occurrences in the string. We encode the string in the following way. First, find two symbols with the smallest probability. No matter whether these two symbols have the same or different probabilities, select them according to their order of occurrence in the B string and then assign the first symbol found to the left node and the second to the right node. Then combine the two symbols found to obtain a new pseudo-symbol and sum the probabilities of the two symbols to represent the probability of this pseudo-symbol. Second, find two symbols with the smallest probability from the generated pseudo-symbols and unprocessed symbols, and then combine them into a new pseudo-symbol. Repeat the above combining actions until all generated pseudo-symbols and unprocessed symbols have been combined into a pseudo-symbol. Finally, there is only one pseudo-symbol left with a probability of 1. It is noted that there may be a choice between two or more symbols/pseudo-symbols with the same probability. If this is the case, for the symbols, please select symbols according to their order of occurrence in the B string. As for the pseudo-symbols, please select according to the order of occurrence of their first component symbols in the B string. 

Assign all the symbols and pseudo-symbols in the form of a binary tree, where each symbol contains a single letter, and each pseudo-symbol may plit up into two smaller pseudo-symbols, two symbols or one pseudo-symbol nd one symbol according to the above combining actions. Label all the left ranches of the tree with a ”0” and all the right branches with a ”1”. The ode for each of the letters is the sequence of 0’s and 1’s that lead to it on he binary tree, starting from the symbol with a probability of 1.

This problem asks you to construct a binary tree for a given B string first ccording to the probability of each symbol. Then encode the B string using he constructed binary tree.

Input Format

The input consists of a number of test cases. For each test case, the first
line contains the number (m, 5 ≤ m ≤ 30) of letters in the B string and the second line lists the B string. Different test cases are separated by a blank line.

Output Format

For a given B string, provide its encoding result. Encoding results of letters in the B string are separated by space. Different test cases are separated by a blank line.

Sample Input 1

5 
YAHOO 

6 
YAGHEE 

10 
ABCBDCDDDD 

Sample Output 1

000 001 01 1 1

000 001 010 011 1 1

000 001 01 001 1 01 1 1 1 1

Hints

Problem Source

Migrated from old NTUJ.

NCPC2007

Subtasks

No. Testdata Range Score

Testdata and Limits

No. Time Limit (ms) Memory Limit (VSS, KiB) Output Limit (KiB) Subtasks
0 1000 65536 200