UVa 12096 - The SetStack Computer

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 |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 // UVa12096 The SetStack Computer

 2 // Rujia Liu

 3 // 代码由刘汝佳提供,我是在我的水平下,加以注释,学习大神的思想

 4 #include<iostream>

 5 #include<string>

 6 #include<set>

 7 #include<map>

 8 #include<stack>

 9 #include<vector>

10 #include<algorithm>

11 using namespace std;

12 

13 #define ALL(x) x.begin(),x.end()

14 #define INS(x) inserter(x,x.begin())

15 

16 typedef set<int> Set;

17 map<Set,int> IDcache; // 把集合映射成ID

18 vector<Set> Setcache; // 根据ID取集合

19 

20 // 查找给定集合x的ID。如果找不到,分配一个新ID

21 int ID (Set x) {

22   if (IDcache.count(x)) return IDcache[x];

23   Setcache.push_back(x); // 添加新集合

24   return IDcache[x] = Setcache.size() - 1;

25 }

26 

27 int main () {

28   int T;

29   cin >> T;

30   while(T--) {

31     stack<int> s; // 题目中的栈

32     int n;

33     cin >> n;

34     for(int i = 0; i < n; i++) {

35       string op;//operation的缩写

36       cin >> op;

37       if (op[0] == 'P') s.push(ID(Set()));//Set()暂且当作空集

38       else if (op[0] == 'D') s.push(s.top());//top取栈顶元素(不删除),push 添加

39       else {

40         Set x1 = Setcache[s.top()]; s.pop();//pop出栈

41         Set x2 = Setcache[s.top()]; s.pop();

42         Set x;

43         if (op[0] == 'U') set_union (ALL(x1), ALL(x2), INS(x));//并集

44         if (op[0] == 'I') set_intersection (ALL(x1), ALL(x2), INS(x));//交集(在<set>中)

45         if (op[0] == 'A') { x = x2; x.insert(ID(x1)); }

46         s.push(ID(x));

47       }      

48       cout << Setcache[s.top()].size() << endl;

49     }

50     cout << "***" << endl;

51   }

52   return 0;  

53 }

37、38、43~45指令只取开头。

你可能感兴趣的:(stack)