Uncompressing Compressed Words
InterviewTime limit1sMemory limit256 MB
Expand each nested compressed word by concatenating its parts and repeating the group n times.
Problem
Steve came up with a way to compress text, though the text does not always get shorter. Steve works on single words only, and defines a "compressed word" with these rules.
- A single lower-case letter is a compressed word.
- is a compressed word, where and are non-negative integers and each is a compressed word.
A compressed word of one character is the same as the uncompressed word. To uncompress , uncompress each , concatenate those uncompressed words in order into a new word, then concatenate that new word times. For example:
xuncompresses tox,(t 3)uncompresses tottt,(a (b c 2) 3)uncompresses toabcbcabcbcabcbc.
Write a program that uncompresses a compressed word.
Input
The input holds one or more test cases. Each test case is one correctly formed compressed word on a line of its own. A $ character marks the end of a line. The last line of the input contains a single $, possibly with leading or trailing spaces, and it is not a test case. Every compressed word in the input follows the rules above. A compressed word may contain leading, trailing, or embedded spaces, and those spaces are ignored. Letters and numbers are separated from each other by at least one space character.
Output
For each test case, print the uncompressed word on a line of its own. The output must contain no spaces other than the line breaks. An uncompressed word can be empty, and an empty word is printed as an empty line.