eolymp
bolt
Try our new interface for solving problems
Problems

Parenthesis

Parenthesis

To a computer, there is no difference between the expression \textbf{(((x)+(y))(t))} and \textbf{(x+y)t}; but, to a human, the latter is easier to read. When writing automatically generated expressions that a human may have to read, it is useful to minimize the number of parentheses in an expression. We assume expressions consist of only two operations: addition (+) and multiplication (juxtaposition), and these operations act on single lower-case letter variables only. Specifically, here is the grammar for an expression \textbf{E}: \begin{verbatim} E : P | P '+' EP : F | F PF : V | '(' E ')'V : 'a' | 'b' | .. | 'z'\end{verbatim}The addition (\textbf{+}, as in \textbf{x+y}) and multiplication (juxtaposition, as in \textbf{xy}) operators are associative: \textbf{x+(y+z)=(x+y)+z=x+y+z} and \textbf{x(yz)=(xy)z=xyz}. Commutativity and distributivity of these operations should not be assumed. Parentheses have the highest precedence, followed by multiplication and then addition. \InputFile The input consists of a number of cases. Each case is given by one line that satisfies the grammar above. Each expression is at most \textbf{1000} characters long. \OutputFile For each case, print on one line the same expression with all unnecessary parentheses removed.
Time limit 1 second
Memory limit 64 MiB
Input example #1
x
(x+(y+z))
(x+(yz))
(x+y(x+t))
x+y+xt
Output example #1
x
x+y+z
x+yz
x+y(x+t)
x+y+xt
Source 2010 Rocky Mountain North America, October 30, Problem A