The SetStack Computer
Time limit1sMemory limit128 MB
Simulate a stack machine whose elements are hereditarily finite sets, printing top-of-stack cardinality after each of five set operations.
- Level
Medium6 of 10
- Topics
- Hash map, Stack, Simulation, Recursion
- Solved
- No attempts yet
Problem
A group of theorists is building a supercomputer that operates on sets instead of numbers. Your job is to simulate its prototype, the SetStack Alpha.
The machine keeps a single stack of sets, which starts out empty. After every operation it prints the cardinality of the set currently on top of the stack. The cardinality of a set , written , is the number of elements it contains.
There are five commands:
PUSH— push the empty set{}onto the stack.DUP— duplicate the top set (pop it, then push it back twice).UNION— pop the top two sets and push their union.INTERSECT— pop the top two sets and push their intersection.ADD— pop the top two sets, insert the first (upper) set as a single element of the second (lower) set, and push the result.
Sets may contain other sets as elements, and two sets with exactly the same elements are considered equal.
As an illustration, suppose the top of the stack is A = { {}, {{}} } and the set just below it is B = { {}, {{{}}} }. Then and , and:
UNIONproduces{ {}, {{}}, {{{}}} }, so the output is3.INTERSECTproduces{ {} }, so the output is1.ADDproduces{ {}, {{{}}}, {{}, {{}}} }, so the output is3.
Input
The first line contains an integer with : the number of test cases.
Each test case begins with a line containing the number of operations with . The next lines each contain one of the five commands.
It is guaranteed that the machine can execute the whole command sequence without ever popping from an empty stack.
Output
For each operation, print a single line containing one integer: the cardinality of the set on top of the stack after that command has executed.
After each test case, print a line containing *** (three asterisks).