CMEXPR - Complicated Expressions

no tags 

The most important activity of ACM is the GSM network. As the mobile phone operator, ACM must build its own transmitting stations. It is very important to compute the exact behaviour of electro-magnetic waves. Unfortunately, prediction of electro-magnetic fields is a very complex task and the formulas describing them are very long and hard-to-read. For example, Maxwell's Equations describing the basic laws of electrical engineering are really tough.

ACM has designed its own computer system that can make some field computations and produce results in the form of mathematic expressions. Unfortunately, by generating the expression in several steps, there are always some unneeded parentheses inside the expression. Your task is to take these partial results and make them "nice" by removing all unnecessary parentheses.


There is a single positive integer T on the first line of input (equal to about 10000). It stands for the number of expressions to follow. Each expression consists of a single line containing only lowercase letters, operators (+, -, *, /) and parentheses (( and )). The letters are variables that can have any value, operators and parentheses have their usual meaning. Multiplication and division have higher priority then subtraction and addition. All operations with the same priority are computed from left to right (operators are left-associative). There are no spaces inside the expressions. No input line contains more than 250 characters.


Print a single line for every expression. The line must contain the same expression with unneeded parentheses removed. You must remove as many parentheses as possible without changing the semantics of the expression. The semantics of the expression is considered the same if and only if any of the following conditions hold:

  • The ordering of operations remains the same. That means "(a+b)+c" is the same as "a+b+c", and "a+(b/c)" is the same as "a+b/c".
  • The order of some operations is swapped but the result remains unchanged with respect to the addition and multiplication associativity. That means "a+(b+c)" and "(a+b)+c" are the same. We can also combine addition with subtraction and multiplication with division, if the subtraction or division is the second operation. For example, "a+(b-c)" is the same as "a+b-c".

You cannot use any other laws, namely you cannot swap left and right operands and you cannot replace "a-(b-c)" with "a-b+c".


Sample Input:


Sample Output:


hide comments
aejaz_ahmed9: 2020-08-10 15:03:46

i tried to solve it without using any tree or data structure just by eliminating brackets program is yielding right output for all the inputs i have tried but still i am getting wrong answer i don't understand what wrong i did

jopdhiwaala: 2020-07-15 14:15:11

Hint:If you are building tree then you should check priority of each operator and add paranthesis based on that.

gol2em: 2020-02-16 09:57:02

The answer should be
Mine used to get a-(b*c+d)

sohit5012: 2019-05-13 14:27:19

Consider using Postfix notation.
Requires a lot of tweaking.

j_b_b_j: 2019-05-10 16:57:17

cases that helped me find mistakes:
correct answers: a/(b+c) and (a+b)*b*f/w

revo: 2019-04-24 21:10:55

I used Expression tree along with modified traversal. Just a pointer to people who might want to solve this going forward and need a starting point.

skandgupta: 2018-06-30 19:40:24

the question is so confusing
do they simply want us to remove the parenthesis and nothing else?
better input examples should've been shown

alan_20210202: 2018-06-04 13:56:46

Transforming the expression into an AST and print it recursively should be a pretty straightforward idea.
I AC with recursive descent parsing.
I don't think any simple algorithm with no auxiliary data structure can still handle this elegantly...

Antonio Roberto Paoli: 2018-01-15 06:19:19

This test case caused me a WA.
Unary operator is not used in this problem.

Last edit: 2018-01-15 07:16:01
quock: 2017-04-28 05:26:39

Getting stuck in solve this problem . What guys do you prefer to solve I means "stack" or other way ?

Added by:adrian
Time limit:5s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: NODEJS PERL6 VB.NET
Resource:ACM Central European Programming Contest, Prague 2000