UVa12096 - The SetStack Computer

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 }
时间: 2024-12-26 13:19:03

UVa12096 - The SetStack Computer的相关文章

UVA12096 - The SetStack Computer(set + map映射)

UVA12096 - The SetStack Computer(set + map映射) 题目链接 题目大意:有五个动作: push : 把一个空集合{}放到栈顶. dup : 把栈顶的集合取出来,在入栈两次. add : 出栈两次,把第一个集合作为一个元素放入第二个集合中,再将第二个集合入栈 union: 出栈两次,取这两个集合的并集,将结果入栈. intersect: 出栈两次,取这两个集合的交集,将结果入栈. 每次执行动作后还需要输出目前栈顶集合的元素个数. 解题思路:这题可以用栈和se

uva12096 The SetStack Computer By sixleaves

代码  1 #include <set> 2 #include <string> 3 #include <vector> 4 #include <map> 5 #include <stack> 6 #include <iostream> 7 #include <algorithm> 8 #define ALL(x) x.begin(), x.end() 9 #define INS(x) inserter(x, x.begi

12096 - The SetStack Computer

The SetStack Computer PS:因为该题排版较麻烦,这里给出OJ网址:UVa12096 - The SetStack Computer 有一个专门为了集合运算而设计的"集合栈"计算机.该机器有一个初始为空的栈,并且支持以下操作. PUSH:空集"{}"入栈. DUP:把当前栈顶元素复制一份后再入栈. UNION:出栈两个集合,然后把二者的并集入栈. INTERSECT:出栈两个集合,然后把二者的交集入栈. ADD:出栈两个集合,然后把先出栈的集合加

UVAOJ 12096 The SetStack Computer(STL的运用)

12096 The SetStack Computer Background from Wikipedia: Set theory is a branch ofmathematics created principally by the German mathe-matician Georg Cantor at the end of the 19th century. Initially controversial, set theory has come to play therole of

UVa 12096 The SetStack Computer

Description Background from Wikipedia: "Set theory is a branch of mathematics created principally by the German mathematician Georg Cantor at the end of the 19th century. Initially controversial, set theory has come to play the role of a foundational

12096 - The SetStack Computer UVA

Background from Wikipedia: \Set theory is a branch of mathematics created principally by the German mathematician Georg Cantor at the end of the 19th century. Initially controversial, set theory has come to play the role of a foundational theory in m

uva 12096 - The SetStack Computer(STL)

UVA 12096 - The SetStack Computer 题目链接 题意:几个操作,push是在栈顶加入一个空集,dup是复制栈顶集合,在放入栈顶,union是把头两个取并集放回,int是头两个取交集放回,add是取头两个,把第一个当成一个集合加入第二个,每次操作输出栈顶集合的里面的个数 思路:用set,stack模拟,然后利用map去hash一个集合,模拟即可 代码: #include <cstdio> #include <cstring> #include <s

UVA - 12096 The SetStack Computer(编码,STL)

12096 The SetStack Computer Background from Wikipedia: "Set theory is a branch of mathematics created principally by the German mathe-matician Georg Cantor at the end of the 19th century.Initially controversial, set theory has come to play the role o

一个FLAG #14# The SetStack Computer

题 集合栈计算机,完整题目见参考[1] 书上的原始代码如下: #include <iostream> #include <vector> #include <map> #include <set> #include <string> #include <stack> #include <algorithm> // algorithm是必要的?否则 set_union报错 using namespace std; typed