Sphere Online Judge

SPOJ Problem Set (classical)

4. Transform the Expression

Problem code: ONP

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
Languages:All except: CLOJ F# PERL 6 TECS
Resource:-

hide comments
2010-08-26 11:29:31 M Rajesh
There is no priority..



Last edit: 2010-08-26 11:29:55
2010-07-29 13:45:27 nikhil
Runtime error NZEC ??????????/
2010-07-16 09:25:26 koma
can it be like a+b ?
or should it be (a+b) ?
2010-07-07 05:08:54 sai nareen chand


Last edit: 2010-07-07 05:09:11
2010-06-15 12:51:52 Anuj Mahajan
can the input be without brackets...?
or
can it be like a+(b+c)

Last edit: 2010-06-15 12:54:04
2010-03-30 20:45:21 Zobayer
@Gene Campbell, the algorithm should work in such way that the presence / absence of () makes no difference...
2010-02-11 06:16:28 Gene
One other question, I take it the ()'s are optional. My solution works locally, but failed to pass, so I'm guessing I need to assume no ()'s
2010-02-11 04:02:41 Gene
Can recursion and regular expressions be use with Python to do this problem?
2009-09-16 04:14:31 Dan Chrostowski
Does the judge allow import statements in Java???? It keeps throwing a compilation error at me when it reads the import statement
2009-08-31 01:12:53 Gabriel Peixoto
I forgot to empty my operand stack after reading the expression and got AC!

Input: a+b
My program answer: ab (without the +)
SPOJ System © 2010 Sphere Research Labs. All Rights Reserved.