#K58917. Reverse Parentheses
Reverse Parentheses
Reverse Parentheses
You are given a string s consisting of lowercase English letters and parentheses. Your task is to reverse the substring inside each pair of matching parentheses. Note that the parentheses may be nested, and the reversal process is applied from the innermost pair outward.
For example, given the input string (u(love)i)
, the substring love
is reversed to form evol
resulting in (u(evol)i)
, and then the entire content inside the outer parentheses is reversed to produce iloveu
.
The relation can be described by the following formula in \( \LaTeX \):
[ result = reverse(s)\quad\text{where } \ reverse(\ldots) \text{ processes innermost parentheses first.} ]
Your solution should read the input from standard input (stdin) and output the result to standard output (stdout).
inputFormat
The input consists of a single line containing the string s that includes lowercase letters and parentheses.
Constraints: \(1 \leq |s| \leq 2000\)
outputFormat
Output a single line containing the resulting string after processing all the reversals inside each pair of parentheses.
## sample(abcd)
dcba