UVa 12096 the Setstack computer

Source: Internet
Author: User

Description

Background from Wikipedia: "Set theory are a branch of mathematics created principally by the German mathematician Georg Ca Ntor at the end of the 19th century. Initially controversial, set theory have come to play the role of a foundational theory in modern mathematics Of a theory invoked to justify assumptions made inmathematics concerning the existence of mathematical objects (such as n Umbers 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 instead of numbers. The initial set-stack Alpha is under construction, and they need your to simulate it on order to verify the operation of th E prototype.

The computer operates on a single stack of sets, which is initially empty. After all operation, the cardinality of the 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'll pops 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 all test case contains the number of operations 0≤n≤2 000. 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 all test case there is a line with * * * (three asterisks).

Sample Input

29pushdupaddpushadddupadddupunion5pushpushaddpushintersect

Sample Output

001011222***00100***
Analysis: Using STL can easily solve this problem. Using vectors to store different sets, map the set to the corresponding subscript in the vector. Set of operations union, INTERSECT, add can be used algorithm in the set_union, set_intersection and set the insert completed, according to test instructions use stack simulation stack operation.
1#include <iostream>2#include <Set>3#include <map>4#include <vector>5#include <stack>6#include <algorithm>7 using namespacestd;8 9map<Set<int,int>ID;Tenmap<Set<int,int>:: iterator it; Onevector<Set<int> >v; A  - intGetID (Set<int>x) - { theit =id.find (x); -     if(It! =id.end ()) -         returnIt->second; - v.push_back (x); +     returnID[X] = v.size ()-1; - } +  A intMain () at { -     intT, N; -Cin>>T; -      while(t--){ -Cin>>N; - id.clear (); in v.clear (); -stack<int>s; to         stringop; +          while(n--){ -Cin>>op; the             if(op[0] =='P') *S.push (GetID (Set<int>())); $             Else if(op[0] =='D')Panax Notoginseng S.push (S.top ()); -             Else{ the                 Set<int> T1 =v[s.top ()]; S.pop (); +                 Set<int> t2 =v[s.top ()]; S.pop (); A                 Set<int>T; the                 if(op[0] =='U'){ + set_union (T1.begin (), T1.end (), T2.begin (), T2.end (), Inserter (T, T.begin ())); - S.push (GetID (t)); $                 } $                 Else if(op[0] =='I'){ - set_intersection (T1.begin (), T1.end (), T2.begin (), T2.end (), Inserter (T, T.begin ())); - S.push (GetID (t)); the                 } -                 Else if(op[0] =='A'){Wuyi T2.insert (GetID (t1)); the S.push (GetID (T2)); -                 } Wu             } -Cout<<v[s.top ()].size () <<Endl; About         } $cout<<"***"<<Endl; -     } -     return 0; -}

UVa 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.