Evaluate Expression in Stacks and Queues
Evaluate Expression in Stacks and Queues
Evaluate Expression
You are given a string S consisting of N characters. Each character is either a
digit from 0 to 9 or an operator out of + , - , and * .
Input Format
The first line of input will contain a single integer T , denoting the number
of test cases.
Each test case consists of multiple lines of input.
The first line of each test case contains a single integer N — the number
of characters in the string.
The next line consists of N characters, where the ith character is either a
digit from 0 to 9 or an operator out of + , - , and * . Consider the string
to be in reverse polish notation.
Output Format
For each test case, output on a new line, an integer denoting the evaluated
expression.
Constraints
1 ≤ T ≤ 100
1 ≤ N ≤ 105
The sum of N over all test cases won't exceed 2 ⋅ 105 .
Sample 1:
Input Output
4 4
3 0
73- 14
7 -2
04*3*0-
https://www.codechef.com/learn/course/stacks-and-queues/LSTACKS02/problems/PREP21 1/1