All problems

Problem 69

Decode String

Medium · 20 pts · String, Stack, Recursion

Problem

A string is encoded with the rule k[text], which means text repeated k times. Encodings can be nested inside each other, as in 2[a3[b]] = abbbabbb. Decode the given string. The input is always valid, and digits only appear as repeat counts (a count can have several digits). A stack works well: when you meet '[', save the current text and the repeat count; when you meet ']', repeat the text built inside the brackets and append it to the saved text. Recursion is another natural solution.

Input

A single encoded string of lowercase letters, digits and brackets.

Output

Print the decoded string.

Constraints

1 <= |S| <= 30; 1 <= k <= 300; the decoded length is at most 100000

Example input

3[a]2[bc]

Example output

aaabcbc

Explanation

"3[a]2[bc]" has two parts: 3[a] decodes to aaa and 2[bc] decodes to bcbc. Joined together, the result is aaabcbc.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Find First Occurrence Next: Word Pattern