ONP - Transform the Expression

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:mima
Date:2004-05-01
Time limit:5s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: NODEJS PERL6 VB.NET
Resource:-

hide comments
2018-08-07 21:11:50
I wrote the code in c++, and for fast input/output, i often use
---ios_base::sync_with_stdio(false);
---cin.tie(NULL);

But by using this, I was getting TLE, which is quite strange. Can anyone help me get what's happening?
2018-08-04 22:32:37
my code is running correctly for given test cases but on submitting showing wrong ans. any particular test case for which I should check.
2018-05-12 19:26:46 daya
my code got AC for the test case a+b-c whose output is abc-+ , but i think the output should be ab+c- , which one is correct ?
2018-04-11 08:04:40
this is bery easy
2018-03-22 15:05:30
0.00 sec in ASM
2018-03-17 09:40:34
Solved without stack data structure !!!
2018-02-08 10:16:33
You can try the Shunting Yard approach, or even go for RD parsing using a LL(k) grammar.
Shunting yard is...best for this kind of question, parsing mathematical expression and such, but kind of useless in real applications. Oh if I do remember correct, its complexity is O(n).
2018-02-05 21:15:08
no need to consider the special cases i.e. when parenthesis are not there.
2018-01-26 21:51:20
Since expression is given in fully parenthesised format (as in examples) there is no need to take care of priorities of operators...
2018-01-16 12:20:18
A stack is not required in this problem. However, it is easier to use a stack!

Last edit: 2018-01-16 12:20:45
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.