码迷,mamicode.com
首页 > 其他好文 > 详细

UVa12096 - The SetStack Computer

时间:2015-04-24 20:52:45      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:

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 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 will push the empty set {} on the stack.
DUP will duplicate the topmost set (pop the stack, and then push that set on the stack twice).
UNION will pop the stack twice and then push the union of the two sets on the stack.
INTERSECT will pop the stack twice and then push the intersection of the two sets on the stack.
ADD will 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 that the topmost element of the stack is
A = {{} , {{}}}
and that the next one is
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 each
test case contains the number of operations 0 N 2000. Then follow N lines each containing one of
the five commands. It is guaranteed that 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 will be 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 each test case there will 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 LYLtim
 2 //2015.4.24
 3 
 4 #include <iostream>
 5 #include <set>
 6 #include <stack>
 7 #include <map>
 8 #include <vector>
 9 #include <algorithm>
10 
11 using namespace std;
12 
13 using Set = set<int>;
14 map<Set, int> IDCache;
15 vector<Set> SetCache;
16 
17 int ID (Set set) {
18     if (IDCache.count(set))
19         return IDCache[set];
20     SetCache.push_back(set);
21     return IDCache[set] = SetCache.size() - 1;
22 }
23 
24 int main() {
25     int t, n;
26     stack<int> s;
27     cin >> t;
28     for (int i = 0; i < t; i++) {
29         cin >> n;
30         for (int j = 0; j < n; j++) {
31             string op;
32             cin >> op;
33             if (op[0] == P)
34                 s.push(ID(Set()));
35             else if (op[0] == D)
36                 s.push(s.top());
37             else {
38                 Set s1 = SetCache[s.top()]; s.pop();
39                 Set s2 = SetCache[s.top()]; s.pop();
40                 Set tmpSet;
41                 if (op[0] == U) {
42                     set_union(s1.begin(), s1.end(), s2.begin(), s2.end(), inserter(tmpSet, tmpSet.begin()));
43                     s.push(ID(tmpSet));
44                 }
45                 else if (op[0] == I) {
46                     set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(), inserter(tmpSet, tmpSet.begin()));
47                     s.push(ID(tmpSet));
48                 }
49                 else if (op[0] == A){
50                     s2.insert(ID(s1));
51                     s.push(ID(s2));
52                 }
53             }
54             cout << SetCache[s.top()].size() << endl;
55         }
56         cout << "***" << endl;
57     }
58 }

 

UVa12096 - The SetStack Computer

标签:

原文地址:http://www.cnblogs.com/LYLtim/p/4454372.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!