UVa 12096 (STL) the Setstack computer

Source: Internet
Author: User

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

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.