12096-the Setstack Computer

Source: Internet
Author: User

The Setstack computer

PS: Because the layout of the problem is more troublesome, here OJ Address: uva12096-the setstack Computer

There is a "collection stack" computer designed specifically for set operations. The machine has a stack that is initially empty and supports the following 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 put the first-out stack of the collection into the back of the stack, the results into the stack.

The size of the top collection of output stacks (that is, the number of elements) after each operation. For example, the top element of the stack is a={{},{{}}, and the next element is b={{},{{{}}}:

  • The union operation will get {{},{{}},{{{}}}, Output 3.
  • The Intersect operation will get {{}}, Output 1.
  • The add operation will get {{},{{{}}},{{},{{}}}, Output 3.

Input no more than 2000 operations, and ensure that the operation can proceed smoothly (do not need to do a stack operation on the empty stack).

#include <iostream>#include <string>#include <stack>#include <set>#include <map>#include <vector>#include <algorithm>using namespace STD;//Design ideas://Map can only get associations such as Key->value//If you want to pass Value->key, you must add another secondary container//In this is achieved through the vector container value->key, that is, the following relationship//IDCACHE[X] is the ID of the collection x//setcache[idcache[x]] is the X collection itself//set<int> is set, int is ID Map<set<int>, int>Idcache;//Cache Set Collection vector<set<int> >Setcache;//Find ID of the given set S, if not found, assign a new IDintID ( Set<int>s) {if(Idcache.count (s)) {returnIdcache[s]; }//Add collection X to the set cacheSetcache.push_back (s);//Set X position in cache as unique ID    returnIdcache[s] = setcache.size ()-1;}intMain () {intTCin>> T; while(t--) {//Collection Stack         Stack<int>SintNCin>> N; for(inti =0; I < n; i++) {//Operation            stringOpCin>> op;//PUSH            if(op[0] ==' P ') {//Put the empty set into the stackS.push (ID ( Set<int>())); }Else if(op[0] ==' D ') {//DUP                //stack top elements into the stack againS.push (S.top ()); }Else{//Remove two elements from the top of the stack                 Set<int>X1 = Setcache[s.top ()]; S.pop (); Set<int>x2 = Setcache[s.top ()]; S.pop (); Set<int>X//UNION                if(op[0] ==' U ') {//x1 The element starting end position iterator for the collection                    the//3,4 parameter is the starting end position iterator for the element of the collection X2                    //5 parameter represents insert iteratorSet_union (X1.begin (), X1.end (), X2.begin (), X2.end (), Inserter (x, X.begin ())); }//INTERSECT                if(op[0] ==' I ') {set_intersection (X1.begin (), X1.end (), X2.begin (), X2.end (), Inserter (x, X.begin ())); }//ADD                if(op[0] ==' A ') {x = x2;                X.insert (ID (x1));            } s.push (ID (x)); }cout<< setcache[s.top ()].size () << Endl; }cout<<"***"<< Endl; }return 0;}

12096-the Setstack Computer

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.