#P6439. Parentheses Removal

    ID: 19653 Type: Default 1000ms 256MiB

Parentheses Removal

Parentheses Removal

Given an arithmetic expression with some parts enclosed in parentheses to indicate precedence, your task is to remove one or more pairs of matching parentheses and output all distinct expressions obtained by such removals in lexicographical order.

Note: When removing parentheses, you must always remove both the opening and its corresponding closing parenthesis. Removal of only one part of a pair is not allowed. Also, the original expression is valid; however, after removal, the expression may look different even though the evaluation order might change.

For example, given the expression

[ (2+(2\times2)+2) ]

all legal removal schemes are:

  • (2+2*2+2)
  • 2+(2*2)+2
  • 2+2*2+2

Any scheme that does not remove a complete matching pair is considered invalid.

inputFormat

The input consists of a single line containing a valid arithmetic expression.

The expression will contain digits, operators (+, -, *, /) and pairs of parentheses ( ) used for grouping.

outputFormat

Output all distinct expressions obtained by removing one or more pairs of matching parentheses, one per line, sorted in lexicographical order.

sample

(2+(2*2)+2)
(2+2*2+2)

2+(22)+2 2+22+2

</p>