首页 > 代码库 > UVa12096
UVa12096
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 of a foundational theory in modern mathematics,
in the sense of a theory invoked to justify assumptions
made in mathematics concerning the existence of mathe-
matical objects (such as numbers or functions) and their
properties. Formal versions of set theory also have a
foundational role to play as specifying a theoretical ideal
of mathematical rigor in proofs."
Given this importance of sets, being the basis of
mathematics, a set of eccentric theorist set off to construct a supercomputer operating on sets in-
stead of numbers. The initial SetStack Alpha is under construction, and they need you to simulate it
in order to verify the operation of the prototype.
The computer operates on a single stack of sets, which is initially empty. After each operation, the
cardinality of the topmost set on the stack is output. The cardinality of a set S is denoted jSj 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 fg 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 rst 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 = ffg; ffggg
and that the next one is
B = ffg; fffgggg
For these sets, we have jAj = 2 and jBj = 2. Then:
UNION would result in the set ffg, ffgg, fffgggg. The output is 3.
INTERSECT would result in the set ffgg. The output is 1.
ADD would result in the set ffg, fffggg, ffg,ffgggg. The output is 3.
Universidad de Valladolid OJ: 12096 { The SetStack Computer 2/2
Input
An integer 0 T 5 on the rst line gives the cardinality of the set of test cases. The rst line of each
test case contains the number of operations 0 N 2000. Then follow N lines each containing one of
the ve 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 specied 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
***
题意:
一个栈,有5种操作;
- PUSH:向栈中放一个空集合。
- DUP:复制栈顶集合。
- UNION:取栈顶的两个集合,取并集后放回。
- INTERSECT:取栈顶的两个集合,取交集后放回。
- ADD:取栈顶两个集合,将第一个集合作为元素放到第二个集合中,并将第二个集合放回栈。
每次操作后输出栈定集合中元素的个数。
分析:
本题的集合是集合的集合,我们为每一个集合分配一个唯一的正整数ID,这样每个集合都可以用set<int>来表示,题目中的栈则可以用stack<int> s来表示。使用字典map IDcache将集合映射成ID,使用容器vector Setcache将ID映射为集合,这样编写一个ID函数容易实现集合与ID的一一对应。注意取交集和并集的时候定义宏ALL(x)以及INS(x)写起来比较好看。
1 #include <set> 2 #include <iostream> 3 #include <algorithm> 4 #include <stack> 5 #include <map> 6 #include <vector> 7 using namespace std; 8 typedef set<int> Set; 9 map<Set,int> IDcache; // 将集合映射为ID10 vector<Set> Setcache; // 根据ID取集合11 // 查找给定集合的ID,如果找不到则为它分配一个新ID12 int ID(Set x){13 if(IDcache.count(x)) return IDcache[x];14 Setcache.push_back(x); // 添加新集合15 return IDcache[x] = Setcache.size() - 1;16 }17 #define ALL(x) x.begin(),x.end() // 所有的内容18 #define INS(x) inserter(x,x.begin()) // 插入迭代器19 stack<int> s; // 题目中的栈20 int n;21 int main(){22 int T; cin >> T;23 while(T--){24 cin >> n;25 for(int i = 0 ; i < n ; i++){26 string op; cin >> op;27 if(op[0] == ‘P‘) s.push(ID(Set())); // Push,入一个空集28 else if(op[0] == ‘D‘) s.push(s.top()); // Dup,加入栈顶元素29 else{30 Set x1 = Setcache[s.top()]; s.pop();31 Set x2 = Setcache[s.top()]; s.pop(); // 出栈两个集合32 Set x; // 将要入栈的新集合33 if(op[0] == ‘U‘) set_union(ALL(x1),ALL(x2),INS(x));34 if(op[0] == ‘I‘) set_intersection(ALL(x1),ALL(x2),INS(x));35 if(op[0] == ‘A‘){36 x = x2;37 x.insert(ID(x1));38 }39 s.push(ID(x));40 }41 cout << Setcache[s.top()].size() << endl;42 }43 cout << "***" << endl;44 }45 }
UVa12096