#C10029. Stack Operations Simulator
Stack Operations Simulator
Stack Operations Simulator
In this problem, you are given a series of operations that are performed on a stack. The allowed operations are:
- P x: Push the integer \(x\) onto the stack.
- Q: Query the current top element of the stack. If the stack is empty, the query returns "EMPTY".
- R: Remove (pop) the top element from the stack. If the stack is empty, do nothing.
- D: Duplicate the current top element of the stack. If the stack is empty, do nothing.
- M x: Multiply the top element of the stack by \(x\) (i.e. update the top element \(a\) to \(a \times x\)). If the stack is empty, do nothing.
The operations are executed sequentially for each test case. Every time a query operation ((Q)) is encountered, record its result. After processing all test cases, output all query results (in the order they appeared) followed by the final state of the stack from the last test case. The final state is printed from the bottom to the top of the stack. If the final stack is empty, output "EMPTY".
You may assume the stack behaves in a Last-In-First-Out (LIFO) manner. Mathematically, if the state of the stack is ([x_1, x_2, \dots, x_k]), then the top element is (x_k).
inputFormat
The input starts with an integer (T) (with (T \ge 1)) representing the number of test cases. For each test case, the first line contains an integer (N) denoting the number of operations. The next (N) lines each contain one operation (i.e. one of the strings described in the problem statement).
outputFormat
Output all query results produced by the (Q) operations across all test cases, each on a new line. Then, in a single line, print the final state of the stack from the last test case with its elements separated by a single space. If the final stack is empty, output the string "EMPTY".## sample
3
4
P 3
Q
R
Q
7
P 10
P 20
Q
M 2
Q
R
Q
5
P 5
D
M 3
Q
R
3
EMPTY
20
40
10
15
5
</p>