Test instructions
There is a collection stack computer, the elements in the stack are all collections, and there are some related operations. Outputs the number of stack elements at the top of each operation.
Analysis:
The problem feels a bit abstract, set can also set set, but the problem with the set of the figure is very appropriate.
By mapping the set to an ID, you can use stack<int> to simulate the stack of stacks in a problem, and then use Vector<set> to index the collection based on the subscript.
The code is short, but it needs to be understood more.
1#include <cstdio>2#include <string>3#include <vector>4#include <stack>5#include <Set>6#include <map>7#include <iostream>8#include <algorithm>9 using namespacestd;Ten OnetypedefSet<int>Set; AMap<set,int>Idcache; -Vector<set>Setcache; - the #defineAll (x) X.begin (), X.end () - #defineINS (x) Inserter (X,x.begin ()) - - intID (Set x) + { - if(Idcache.count (x))returnIdcache[x]; + setcache.push_back (x); A returnIDCACHE[X] = setcache.size ()-1; at } - - intMain () - { - //freopen ("In.txt", "R", stdin); - in intT; -scanf"%d", &T); to while(t--) + { -stack<int>s; the intN; *scanf"%d", &n); $ for(inti =0; I < n; ++i)Panax Notoginseng { - stringop; theCIN >>op; + if(op[0] =='P') S.push (ID (Set () )); A Else if(op[0] =='D') S.push (S.top ()); the 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)); the if(op[0] =='A') {x =x2; X.insert (ID (x1));} - S.push (ID (x));Wuyi } theprintf"%d\n", Setcache[s.top ()].size ()); - } Wu -Puts"***"); About } $ - return 0; -}code June
UVa 12096 (STL) the Setstack computer