Title Link: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=42064
#include <iostream> #include <algorithm> #include <string> #include <map> #include <set># Include <vector> #include <stack> #define ALL (x) X.begin (), X.end () #define INS (x) Inserter (X,x.begin ()) Using namespace std;/******************************************************************************************* Test instructions: Using stacks to simulate some of the operational learning: 1, variable-length array vectors, set set, mapping map, stack stack integrated application, very good 2, A, using set conforming to the characteristics of the set of container B, using the map to set a unique number ID C for each collection, using the indefinite long array and the collection number ID can easily access to each set D, the number of each collection into the stack, Save processing is very convenient 3, algorithm library inside the built-in intersection, and set functions, note the parameters and usage 4, it is difficult to think how to deal with the empty set, and finally understand that the empty box is also a special collection, in the global definition of an SE T, treat it as a special collection, initialize the empty set numbered-1 5, a trick: For each instruction entered, if the first letter can be used as a unique identifier character. Then use op[0] = = ' P ' instead of op = = "PUSH"; this may reduce consumption, after all, compare two strings to call the C + + library function ************************************************** *************************************************************/set<int> set; Set, using Set 1, non-repeatability (exactly the nature of the collection) Map<set<int>,int > ID; The collection and collection number one by one correspond, each set has a unique number. Empty set numbered -1vector<set<int> > setcache; Take the collection according to the ID int fuc (set<int> x) {if (Id.count (x))///If the set X has been saved, return the number directly to id[x]; Setcache.push_back (x); Otherwise the collection is set into the queue Setcache and the map collection number is return Id[x]=setcache.size ()-1; Map collection number, starting with 0}int main () {int T; cin>>t; while (t--) {int n; cin>>n; Stack <int> s; Stack, put the collection number into the stack for (int i = 1;i <= n;i + +) {string op; cin>>op; if (op[0] = = ' P ') s.push (fuc (Set)); else if (op[0] = = ' D ') S.push (S.top ()); else{set<int> x1=setcache[s.top ()]; S.pop (); Set<int> x2=setcache[s.top ()]; S.pop (); Set<int> x; if (op[0] = = ' U ') set_union(All (x1), All (x2), INS (x)); Built-in Set function if (op[0] = = ' I ') set_intersection (All (x1), All (x2), INS (x)); Built-in intersection function if (op[0] = = ' A ') {x=x2; X.insert (FUC (x1)); } s.push (Fuc (x)); } cout<<setcache[s.top ()].size () <<endl; } cout<< "* * *" <<endl; } return 0;}
UVa-12096 the Setstack computer (STL container synthesis, strong push!) )