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 modern mathematics, in the sense of a theory invoked to justify assumptions made inmathematics concerning the existence of mathematical 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 instead of numbers. The initial Set-Stack 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 ≤ 2 000. 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).
Examples
Input

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

Output

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


 #include <iostream>
#include <map>
#include <set>
#include <algorithm>
#include <stack>
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
using namespace std;
typedef set<int> Set;
map<Set,int> IDcache;
vector<Set> Setcache;
int ID(Set x){
if(IDcache.count(x)) return IDcache[x];
Setcache.push_back(x);
return IDcache[x]=Setcache.size()-;
}
int main()
{
int n,ord;cin>>n;
stack<int> s;
while(n--){
cin>>ord;
while(ord--){
string op;
cin>>op;
if(op[]=='P') s.push(ID(Set()));
else if(op[]=='D') s.push(s.top());
else{
Set x1=Setcache[s.top()];s.pop();
Set x2=Setcache[s.top()];s.pop();
Set x;
switch(op[]){
case 'U': set_union(ALL(x1),ALL(x2),INS(x));break;
case 'I': set_intersection(ALL(x1),ALL(x2),INS(x));break;
case 'A': x=x2;x.insert(ID(x1));break;
}
s.push(ID(x));
}
cout << Setcache[s.top()].size()<<endl;
}
cout<<"***"<<endl; } return ;
}

本题的集合并不是简单的整数集合或字符串集合,而是集合的集合。map为每个不同的集合分配一个唯一的ID,每个key的value是key这个集合的ID,每个集合都可以表示成所包含元素的ID集合,这样就可以用set<int>表示,而整个栈则是一个stack<int>,每一次将集合的ID推入。vec数组方便根据ID取集合。

26行的ID(Set())应该是空集的ID,为0。

举个例子,

第一次PUSH,ID()为空集分配ID——0,并保存入map中,栈推入空集的ID:0;

第二次DUP,将栈顶的集合ID:0再推入栈;

第三次ADD,出栈两个元素,都是空集,ID均为0,将第一个集合加入第二个集合里,即是将第一个集合ID插入到第二个集合中,并给新集合:{0}分配ID——1;并将新集合ID推入栈。则栈顶集合ID:1,集合内元素{0},元素个数:1。

第一次 map:{} 0         vector [0]: 空          stack:0

第二次 map:{} 0         vector [0]: 空          stack:0 0

第三次 map:{} 0,{0},1     vector [0]: 空,[1]:{0}        stack:0 0 1

感觉说的还不是很清楚,多读代码理解吧!

最新文章

  1. 常用js函数封装
  2. Python微信-- 分享接口(分享到朋友圈、朋友、空间)
  3. 怎么给我的Office文档加密
  4. 安装Django,运行django-admin.py startproject 工程名,后不出现指定的工程解决办法!!
  5. Load Mental Ray in Maya 2015
  6. JSR 303标准
  7. 当kfreebsd 用户遇见openSUSE系统
  8. mysql乱码以及Data too long for column全解(最完整实用版)
  9. web页面动态加载UserControl,并调用用户控件中的方法来初始化控件
  10. Reporting Service 报表报 rsReportServerDatabaseError 错的解决方法
  11. ubuntu 14.04安装quickbuild buildagent (二)
  12. Google App Engine 学习和实践
  13. LeetCode_Merge Two Sorted Lists
  14. JavaScript Function arguments.callee caller length return
  15. [Swift]LeetCode416. 分割等和子集 | Partition Equal Subset Sum
  16. Python并发编程之实战异步IO框架:asyncio 下篇(十一)
  17. 注册测绘师20180301-CNSS
  18. 【javaScript基础】异常处理
  19. OpenCV从2到3的过渡
  20. 【托业】【全真题库】TEST01-03-阅读题

热门文章

  1. JS——math
  2. centOS7卸载google-chrome
  3. 如何用windbg查看_eprocess结构
  4. JAR包中读取资源文件
  5. api 签名算法
  6. IDEA 基本配置
  7. 6.shell脚本
  8. python利用7z批量解压rar
  9. uva 10048 Audiophobia UVA - 10048
  10. BZOJ 4430 Guessing Camels赌骆驼