ONP - Transform the Expression

no tags 

Transform the algebraic expression with brackets into RPN form (Reverse Polish Notation). Two-argument operators: +, -, *, /, ^ (priority from the lowest to the highest), brackets ( ). Operands: only letters: a,b,...,z. Assume that there is only one RPN form (no expressions like a*b*c).

Input

t [the number of expressions <= 100]
expression [length <= 400]
[other expressions]

Text grouped in [ ] does not appear in the input file.

Output

The expressions in RPN form, one per line.

Example

Input:
3
(a+(b*c))
((a+b)*(z+x))
((a+t)*((b+(a+c))^(c+d)))

Output:
abc*+
ab+zx+*
at+bac++cd+^*

Added by:Michał Małafiejski
Date:2004-05-01
Time limit:5s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel Pentium G860 3GHz)
Languages:All except: NODEJS PERL 6 VB.net
Resource:-

hide comments
Amritendu Mondal: 2015-08-10 11:17:21

I didn't use stack but a tree to store the expression. Good problem , happy to solve this type.

gaurav: 2015-08-08 09:48:11

Are there any boundary conditions we need to check for?
I have not included the operator precedence, however have tested the solution for a range of expressions and it gives the right answer. SPOJ says its wrong. Are there any conditions I need to check for ? Could someone give me example cases?

MishThi: 2015-08-05 11:46:31

Taught me about how stacks function. A great problem for beginners to learn about Stacks. Also, my 49th. :D

c_lauw97: 2015-08-01 07:18:47

if the input was (a+b^c) , the output should be abc^+ ? true or not? need explanation for several test case

sudomudo: 2015-07-19 23:45:01

Use stack, and don't forget to format the output the right way. GG :-)

treyshaffer: 2015-07-19 00:37:02

A good algorithm for solving this problem:
https://en.wikipedia.org/wiki/Shunting-yard_algorithm

seamel_1: 2015-07-16 16:18:53

Could someone tell me what is wrong in my code http://pastebin.com/fG8x8QTC compile and works well but spoj detects runtime error (SIGSEGV)

Supreme: 2015-07-14 13:52:23

Tried solving without using stacks - didnt get the solution even after 2 days.
Tried solving implementing stacks - got the solution within 1.5 hour.

Got to know the power & importance of stack.

himanshu kumar: 2015-07-13 18:25:28

please do see the priority hehe... giving me one wrong answer :p

artista_14: 2015-06-28 15:09:30

STL made code shorter......Excellent problem for beginners.......


Problem's scores 1 vote

Concept difficulty
Concept difficulty 21%
Implementation difficulty
Implementation difficulty 13%
24