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

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

EOJ 1641/UVa The SetStack Computer的更多相关文章

  1. UVA.12096 The SetStack Computer ( 好题 栈 STL混合应用)

    UVA.12096 The SetStack Computer ( 好题 栈 STL混合应用) 题意分析 绝对的好题. 先说做完此题的收获: 1.对数据结构又有了宏观的上的认识; 2.熟悉了常用STL ...

  2. 12096 - The SetStack Computer UVA

    Background from Wikipedia: \Set theory is a branch of mathematics created principally by the German ...

  3. UVa12096.The SetStack Computer

    题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  4. 集合栈计算机(The SetStack Computer, ACM/ICPC NWERC 2006,Uva12096)

    集合栈计算机(The SetStack Computer, ACM/ICPC NWERC 2006,Uva12096) 题目描述 有一个专门为了集合运算而设计的"集合栈"计算机.该 ...

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

    UVA12096 - The SetStack Computer(set + map映射) 题目链接 题目大意:有五个动作: push : 把一个空集合{}放到栈顶. dup : 把栈顶的集合取出来, ...

  6. uva 12096 - The SetStack Computer(集合栈)

    例题5-5 集合栈计算机(The Set Stack Computer,ACM/ICPC NWERC 2006,UVa12096) 有一个专门为了集合运算而设计的"集合栈"计算机. ...

  7. uva 12096 The SetStack Computer

    点击打开链接uva 12096 思路: STL模拟 分析: 1 题目给定5种操作,每次输出栈顶集合的元素的个数 2 利用stack和set来模拟,set保存集合的元素.遇到push的时候直接在stac ...

  8. UVa 12096 The SetStack Computer【STL】

    题意:给出一个空的栈,支持集合的操作,求每次操作后,栈顶集合的元素个数 从紫书给的例子 A={{},{{}}} B={{},{{{}}}} A是栈顶元素,A是一个集合,同时作为一个集合的A,它自身里面 ...

  9. UVa 12096 (STL) The SetStack Computer

    题意: 有一个集合栈计算机,栈中的元素全部是集合,还有一些相关的操作.输出每次操作后栈顶集合元素的个数. 分析: 这个题感觉有点抽象,集合还能套集合,倒是和题中配的套娃那个图很贴切. 把集合映射成ID ...

随机推荐

  1. Windows Server 2008无法远程连接

    Server 2008 R2依次配置好之后,重启发现总是远程桌面时而连接不上.具体现象如下: 偶尔可以通过桌面远程连接连接到Server.以为是防火墙的问题,各种设置——甚至关闭,依然无法连接.反复重 ...

  2. NGINX+PHP-FPM7 FastCGI sent in stderr: “Primary script unknown”

    https://www.cnblogs.com/hjqjk/p/5651275.html 一开始是Nginx打开网页显示一直是拒绝访问.查看nginx日志是报错显示我的题目,然后就各种搜索解决啊! 百 ...

  3. 哈夫曼树(Huffman Tree)

    Date:2019-06-21 14:42:04 做题时更多的是用到哈夫曼树的构造思想,即按照问题规模从小到大,依次解决问题,可以得到最优解 Description: 在一个果园里,多多已经将所有的果 ...

  4. wing ide破解

    LicenseID='CN123-12345-12345-67891' # RequestCode='RL634-8363J-X7E8K-95XD3' RequestCode = 'RW61C-NN6 ...

  5. Echarts特效散点图全解

    mytextStyle={ color:"#333", //文字颜色 fontStyle:"normal", //italic斜体 oblique倾斜 font ...

  6. [SQL Service] 时间处理:获取今天的00:00:00/获取今天的23:59:59

    获取今天的00:00:00 SELECT CONVERT(DATETIME,CONVERT(VARCHAR(10),GETDATE(),120)) 获取今天的23:59:59 1.SELECT DAT ...

  7. Redis防护建议

    1.Redis本身防护  (1)不要使用默认端口(6379)  (2)增加Redis用户名和密码  (3)在Redis绑定指定IP访问(位置配置文件[redis.config]中的bind节点)2.L ...

  8. postgres主从配置

    运维开发技术交流群欢迎大家加入一起学习(QQ:722381733) 开始部署postgres主从(如果没不会安装postgres的请去上一个博文中查看) 这里我使用了两台服务器部署 主:192.168 ...

  9. PAT 1111 Online Map

    Input our current position and a destination, an online map can recommend several paths. Now your jo ...

  10. 一个电商项目的Web服务化改造3:改进方案の规范和约定、单表、单一职责

         最近一直在做一个电商项目,需要把原有单系统架构的项目,改造成基于服务的架构,SOA.      有点挑战,做完了,会有很大进步. 上一篇,我们描述了原有项目中的问题.  或者说是,本篇的基本 ...