eolymp
bolt
Try our new interface for solving problems
Problems

Decompressing in a GIF

Decompressing in a GIF

One well known method to compress image files is the Graphics Interchange Format (GIF) encoding, created by CompuServe in \textbf{1987}. Here’s a simplified version applied to strings of alphabetic characters. Essential for this compression is a dictionary which assigns numeric encodings (we’ll use base \textbf{10} numbers for this problem) to different strings of characters. The dictionary is initialized with mappings for characters or substrings which may appear in the string. For example, if we expect to encounter all \textbf{26} letters of the alphabet, the dictionary will initially store the encodings (\textit{\textbf{A}}, \textbf{00}), (\textit{\textbf{B}}, \textbf{01}), (\textit{\textbf{C}}, \textbf{02}), . . . , (\textit{\textbf{Z}}, \textbf{25}). If we are compressing DNA data, the dictionary will initially store only \textbf{4} entries: (\textit{\textbf{A}}, \textbf{0}), (\textit{\textbf{T}}, \textbf{1}), (\textit{\textbf{G}}, \textbf{2}) and (\textit{\textbf{C}}, \textbf{3}). Note that the length of each initial encoding is the same for all entries (\textbf{2} digits in the first example, and \textbf{1} digit in the second). The compression algorithm proceeds as follows: \begin{enumerate} \item Find the longest prefix of the uncompressed portion of the string which is in the dictionary, and replace it with its numeric encoding. \item If the end of the string has not been reached, add a new mapping (\textit{\textbf{s}}\textit{, }\textit{\textbf{n}}) to the dictionary, where \textit{\textbf{s}} = the prefix just compressed plus the next character after it in the string, and \textit{\textbf{n}} = the smallest number not yet used in the dictionary. \end{enumerate} For example, assume we started with the string \textbf{ABABBAABB} and a dictionary with just two entries, (\textit{\textbf{A}}, \textbf{0}) and (\textit{\textbf{B}}, \textbf{1}). The table below shows the steps in compressing the string. The final compressed string is \textbf{01234}. There is only one other rule: the replacement strings used are always the size of the longest encoding in the dictionary at the time the replacement occurs. Thus, with the dictionary above, if the string to compress is long enough that an entry of the form (\textit{\textbf{s}}, \textbf{10}) is added to the dictionary, then from this point on all numerical replacement strings used in the compressed string must be expanded to \textbf{2} digits long (i.e., \textbf{A} will now be encoded as \textbf{00}, \textbf{B} as \textbf{01}, \textbf{AB} as \textbf{02}, etc.); if an entry (\textit{\textbf{s}}\textit{′}, \textbf{100}) is added to the dictionary, all replacements from this point forward will increase to \textbf{3} digits long, and so on. Thus, the longer string \textbf{ABABBAABBAABAABAB} will be encoded as \textbf{01234027301}, not \textbf{0123402731}. Try it! OK, now that you are experts at compressing, it’s time to relax and decompress! \InputFile Each test case will consist of two lines. The first line will contain a string of digits to decompress. The second line will contain the initial dictionary used in the compression. This line will start with a positive integer \textit{\textbf{n}} indicating the number of entries in the dictionary (\textbf{1} ≤ \textit{\textbf{n}} ≤ \textbf{100}), followed by \textit{n} alphabetic strings. The first of these will be paired with \textbf{0} in the dictionary (or \textbf{00} if\textbf{ }\textit{n} > \textbf{10}), the second with \textbf{1}, and so on. The last test case will be followed by a line containing a single \textbf{0}. \OutputFile For each test case, output a single line containing the case number (using the format shown below) followed by the decompressed string. All input strings will have been legally compressed.
Time limit 1 second
Memory limit 32 MiB
Input example #1
01234
2 A B
01234027301
2 A B
02151120182729
26 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 
21104
3 BA A C
01
2 JA VA
0
Output example #1
Case 1: ABABBAABB
Case 2: ABABBAABBAABAABAB
Case 3: CPLUSPLUS
Case 4: CAABAAA
Case 5: JAVA