12096 the Setstack computer
Background from Wikipedia: "Set theory are a branch of mathematics created principally by the German Mathe-matician Georg C Antor at the end of the 19th century. Initially controversial, set theory have come to play the role of a foundational theory in modern mathematics,in the sense Of a theory invoked to justify assumptions made in mathematics concerning the existence of Mathe-matical objects (such as Numbers or functions) and their properties. Formal versions of Set theory also has a foundational role to play as specifying a theoretical ideal of mathematical Rigo R in Proofs. " Given This importance of sets, being the basis of mathematics, a set of eccentric theorist set off to construct a supercom Puter operating on sets In-stead of numbers. The initial setstack Alpha is under construction, and they need your to simulate it on order to verify the operation of the Prototype. The computer operates on a single stack of sets, which is initially empty. After each operation, the cardinality ofThe topmost set on the stack is output. The cardinality of a set S is denoted | s| and is the number of elements in S. The instruction set of the Setstack Alpha is PUSH, DUP, UNION, Intersect,and ADD.
? Push would push the empty set {} on the stack.
? DUP would duplicate the topmost set (pop the stack, and then push this set on the stack twice).
? Union would pops the stack twice and then push the union of the sets on the stack.
? INTERSECT'll pops the stack twice and then push the intersection of the both sets on the stack.
? Add would pop the stack twice, add the first set to the second one, and then push the resulting set
On the stack.
For illustration purposes, assume this topmost element of the stack is
A = {{},{{}}}
And that the next one are
B = {{},{{{}}}}
For these sets, we have | a| = 2 and | b| = 2. Then:
? UNION would result in the set {{}, {{}}, {{{}}}}. The output is 3.
? INTERSECT would result in the set {{}}. The output is 1.
? ADD would result in the set {{}, {{}}}, {{},{{}}}. The output is 3.
Input
An integer 0≤t≤5 on the first line gives the cardinality of the set of test cases. The first line of Eachtest case contains the number of operations 0≤n≤2000. Then follow N lines each containing one of the five commands. It is guaranteed, the Setstack computer can execute all the commands in the sequence without ever popping an empty STA Ck.
Output
For each operation specified in the input, there'll be is one line of output consisting of a single integer. This was the cardinality of the topmost element of the stack after the corresponding command had executed. After the test case there'll be a line with ' * * * ' (three asterisks).
sample input
2
9
push
DUP
add
push
ADD
dup
add
DUP
union
5
PUSH
push
add
PUSH
intersect
Sample Output
0
0
1
0
1
1
2
2
2
***
0
0
1
0
0
***
The main idea of the problem: set operations
There are 5 types of operations:
Push: Empty set "{}" into the stack.
DUP: Copies the top element of the current stack and then into the stack.
UNION: Stacks up to two sets, and then sets the two together into the stack.
INTERSECT: Two sets of stacks, and then the intersection of the two into the stack
Add: Out of the stack of two sets, and then add the first out of the stack to the back of the stack, the results into the stack.
The size of the top collection of the output stack after each operation.
is a collection of collections that, in order to represent different collections, can be represented by an integer ID, such as 1 for {}, the set {{}} to be represented as {1}, and 2 as its ID. So the topic becomes the coding problem, for each new generated collection, we determine whether the collection has occurred, if not, give him a new ID, has appeared in the existing ID expressed. All collections use a map to represent the corresponding relationship of the collection to the ID, with a queue to store the collection so that it can be set by ID.
#include <iostream> #include <set> #include <vector> #include <stack> #include <map># include<string> #include <cstring> #include <algorithm>using namespace std; #define ALL (x) X.begin (), X.end ()//= All content # define INS (x) Inserter (X,x.begin ())//Insert iterators typedef set<int> SET;MAP<SET, INT>IDC Ache Map the set into idvector<set>setcache; Takes the collection by ID//finds the ID of the given collection X. If not found, assign a new Idint ID (Set x) {if (Idcache.count (x)) return idcache[x]; Setcache.push_back (x); Add new collection return idcache[x] = Setcache.size ()-1;} int main () {int casen;cin >> casen;while (casen--) {idcache.clear (); Setcache.clear ();stack<int>s; Collection stack int n;cin >> n;for (int i = 0; i < n; i++) {string op;cin >> op;if (op[0] = = ' P ') s.push (ID (Set ())); Els E if (op[0] = = ' D ') S.push (S.top ()); Else{set x1 = Setcache[s.top ()]; S.pop (); Set x2 = Setcache[s.top ()]; S.pop (); Set x;if (op[0] = = ' U ') set_union (All (x1), All (x2), INS (x)), if (op[0] = = ' I ') set_intersection (All (x1), All (x2), INS (x)), if (op[0] = = ' A ') {x = x2; X.insert (ID (x1));} S.push (ID (x));} cout << setcache[s.top ()].size () << Endl;} cout << "* * *" << Endl;}}
UVA-12096 the Setstack computer (code, STL)