This page is still under construction.

Parts of this page are still being built. What you see may change.

Uncompressing Compressed Words

Interview

Time limit1sMemory limit256 MB

Summary
Expand each nested compressed word by concatenating its parts and repeating the group n times.
Level

Medium4 of 10

Topics
Recursion, Stack, String
Solved
No attempts yet

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.

  1. A single lower-case letter is a compressed word.
  2. (e1 e2 … et n)(e_1\ e_2\ \dots\ e_t\ n) is a compressed word, where tt and nn are non-negative integers and each eie_i is a compressed word.

A compressed word of one character is the same as the uncompressed word. To uncompress (e1 e2 … et n)(e_1\ e_2\ \dots\ e_t\ n), uncompress each eie_i, concatenate those uncompressed words in order into a new word, then concatenate that new word nn times. For example:

  • x uncompresses to x,
  • (t 3) uncompresses to ttt,
  • (a (b c 2) 3) uncompresses to abcbcabcbcabcbc.

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.

Examples1

  1. Example 1

    Input
    x$
    (t 3)$
    ( a ( b c 2 )   3)     $
    $
    
    Expected output
    x
    ttt
    abcbcabcbcabcbc