0% found this document useful (0 votes)
10 views1 page

Evaluate Expression in Stacks and Queues

The document describes a problem where a string in reverse polish notation needs to be evaluated, consisting of digits and operators (+, -, *). It outlines the input format, including the number of test cases and the structure of each test case. The output should be the evaluated result for each test case on a new line, with constraints provided for the number of characters and test cases.

Uploaded by

Asif Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views1 page

Evaluate Expression in Stacks and Queues

The document describes a problem where a string in reverse polish notation needs to be evaluated, consisting of digits and operators (+, -, *). It outlines the input format, including the number of test cases and the structure of each test case. The output should be the evaluated result for each test case on a new line, with constraints provided for the number of characters and test cases.

Uploaded by

Asif Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

1/26/25, 4:14 PM 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 * .

Consider the string to be in reverse polish notation consisting of digits (from


1 to 9) as the operands and + , - , or * as the operators and evaluate the
expression.

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

You might also like