Uva12096-the Setstack Computer

Source: Internet
Author: User

The computer op erates on a single stack of sets, which is initially empty. After each op eration, the

Cardinality of the topmost set on the stack is output. The cardinality of a setSis denoted| S| and is the
Number of elements inS. 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 would pop 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 0T5 on the first line gives the cardinality of the set of test cases. The first line of each
Test case contains the number of operations 0NFollow then.NLines 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 stack.
Output
For each operation specified in the input, there'll be is one line of output consisting of a single integer.
This integer is the cardinality of the topmost element of the stack after the corresponding command
Has executed. After the test case there'll be a line with ' * * * ' (three asterisks).
Sample Input

2
9
PUSH
DUP
ADD
PUSH
ADD
DUP
ADD
DUP
UNION
5
PUSH
PUSH
ADD
PUSH
INTERSECT


Sample Output

0
0
1
0
1
1
2
2
2
***
0
0
1
0
0
***

1 //by Lyltim2 //2015.4.243 4#include <iostream>5#include <Set>6#include <stack>7#include <map>8#include <vector>9#include <algorithm>Ten  One using namespacestd; A  - usingSet =Set<int>; -Map<set,int>Idcache; theVector<set>Setcache; -  - intID (SetSet) { -     if(Idcache.count (Set)) +         returnidcache[Set]; -Setcache.push_back (Set); +     returnidcache[Set] = Setcache.size ()-1; A } at  - intMain () { -     intt, N; -stack<int>s; -CIN >>T; -      for(inti =0; I < T; i++) { inCIN >>N; -          for(intj =0; J < N; J + +) { to             stringop; +CIN >>op; -             if(op[0] =='P') the S.push (ID (Set ())); *             Else if(op[0] =='D') $ S.push (S.top ());Panax Notoginseng             Else { -Set S1 =setcache[s.top ()]; S.pop (); theSet s2 =setcache[s.top ()]; S.pop (); + Set Tmpset; A                 if(op[0] =='U') { the set_union (S1.begin (), S1.end (), S2.begin (), S2.end (), Inserter (Tmpset, Tmpset.begin ())); + S.push (ID (Tmpset)); -                 } $                 Else if(op[0] =='I') { $ set_intersection (S1.begin (), S1.end (), S2.begin (), S2.end (), Inserter (Tmpset, Tmpset.begin ()));  - S.push (ID (Tmpset)); -                 } the                 Else if(op[0] =='A'){ - S2.insert (ID (S1));Wuyi s.push (ID (S2)); the                 } -             } Wucout << setcache[s.top ()].size () <<Endl; -         } Aboutcout <<"***"<<Endl; $     } -}

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