题干:

Background from Wikipedia: 揝et 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).

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表示,比如说用1表示{},集合{{}}就表示成{1},再用2作为其ID。于是题目就成了编码问题,对每个新生成的集合,我们判断该集合是否出现过,若未出现过,就给他分配一个新的ID,出现过的用已有的ID表示。所有集合用map表示集合与ID的对应关系,用一个队列存集合,以便根据ID去集合。

于竞赛,这种题目还是不太可能碰到的、、于学习C++这门语言与掌握STL,这倒是个不错的题目。

对于那个置空,我们可以用C++中类的概念解释,写成if(op[0] == 'P') sk.push(getid(kong)); 或者因为我们知道他一定是1,所以也可以直接sk.push( 1 );

AC代码:(795ms)

#include<bits/stdc++.h>using namespace std;
int top;
map<set<int>,int> s_i;
map<int,set<int> > i_s;
int getid(set<int> s) {if(s_i.find(s) != s_i.end()) return s_i[s];s_i[s] = ++top;i_s[top] = s;return top;
}int main()
{
//  freopen("in.txt","r",stdin);int t,m;char op[10];set<int> kong,st1,st2,st;kong.clear();cin>>t;while(t--) {top = 0;s_i.clear();i_s.clear();stack<int> sk;scanf("%d",&m);while(m--) {scanf("%s",op);if(op[0] == 'P') sk.push(getid(kong)); else if(op[0] == 'D') sk.push(sk.top());else {st1 = i_s[sk.top()];sk.pop();st2 = i_s[sk.top()];sk.pop();st.clear();//这一句必须加上,不然就waif(op[0] == 'U') {set_union(st1.begin(),st1.end(),st2.begin(),st2.end(),inserter(st,st.begin()));}else if(op[0] == 'I') set_intersection(st1.begin(),st1.end(),st2.begin(),st2.end(),inserter(st,st.begin()));else {st2.insert(getid(st1));st = st2;}sk.push(getid(st));}printf("%d\n",i_s[sk.top()].size());}printf("***\n");}return 0 ;
}

【HDU - 1968】【UVA - 12096】The SetStack Computer (模拟,集合求交集并集操作,STL实现)相关推荐

  1. 【ICPC-369】uva 12096 The SetStack Computer

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

  2. UVa 12096 The SetStack Computer 【STL】【stack】

    题目链接:点击打开链接 紫书116页 AC代码: #include <cstdio> #include <cstring> #include <algorithm> ...

  3. 12096 - The SetStack Computer

    The SetStack Computer PS:因为该题排版较麻烦,这里给出OJ网址:UVa12096 - The SetStack Computer 有一个专门为了集合运算而设计的"集合 ...

  4. UVa12096.The SetStack Computer

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

  5. The SetStack Computer

    原题及翻译 Background from Wikipedia: "Set theory is a branch of mathematics created principally by ...

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

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

  7. uva计算机水平,UVA 12096 集合栈计算机

    UVA 12096 集合栈计算机 题目描述 有一个专门为了集合运算而设计的"集合栈"计算机.该机器有一个初始为空的栈,并且 支持以下操作. PUSH:空集"{}" ...

  8. C# WPF 中用代码模拟鼠标和键盘的操作

    C# WPF 中用代码模拟鼠标和键盘的操作 原文:C# WPF 中用代码模拟鼠标和键盘的操作 原文地址 C#开发者都知道,在Winform开发中,SendKeys类提供的方法是很实用的.但是可惜的是, ...

  9. [Codeforces 555E]Case of Computer Network(Tarjan求边-双连通分量+树上差分)

    [Codeforces 555E]Case of Computer Network(Tarjan求边-双连通分量+树上差分) 题面 给出一个无向图,以及q条有向路径.问是否存在一种给边定向的方案,使得 ...

最新文章

  1. 机器学习数据拆分_解释了关键的机器学习概念-数据集拆分和随机森林
  2. BTC.com率先发起使用开放联盟网关协议OFGP,联合iBitcome钱包携手送糖果
  3. 大牛深入讲解!java数组冒泡排序从小到大
  4. elasticsearch 客户端工具_万字长文:详解 Spring Boot 中操作 ElasticSearch
  5. 编程之美-找到符合条件的整数
  6. 获取当前经纬度php腾讯地图,PHP腾讯地图经纬度转百度地图经纬度
  7. 【PP操作手册】运行MRP产生计划订单
  8. Linux下查看文件和文件夹大小的df和du命令
  9. Ubuntu关闭防火墙
  10. 关于Android4.4的图片路径获取,如果回来的Uri的格式有两种
  11. POJ 1862: Stripies
  12. 裴蜀定理(详细定义+应用+模板)
  13. 无任何编程基础的人,该怎么入门编程?
  14. vim的安装以及基础使用方法;
  15. 高等数学错题集:第133题:连续,偏导数,可微之间的判断关系
  16. 无线蓝牙耳机手机端app开发_不输AirPods,只花百元就能买到的超级耳机,值了!...
  17. element 前端布局理解经验及好用的属性
  18. 口碑发布码战略,CEO范驰认为下一个十年属于新店商
  19. 这样做框架结构图,让你的PPT更有创意!
  20. 美国,华尔街,某大银行。

热门文章

  1. [安全模型][Cambria Math][A][]敌手A-> 怎么打出来?
  2. PAT1130. Infix Expression (25) 中序遍历
  3. linux系统命令光标移动,Linux 命令行 光标移动技巧及利用grep和find查找文件内容...
  4. PHP水果店管理系统,水果店连锁店管理系统实现一体化功能
  5. 5g pdu session_运营商下架4G套餐,用户被5G!
  6. java 打印 模板_Java输入输出模板
  7. mysql binlog sql统计_mysql的binlog详解
  8. php关闭按钮,C#_winform去掉右上角关闭按钮的方法,一种方法是可以在窗体的属性 - phpStudy...
  9. 清华大学-曾鸣-《ARM微控制器与嵌入式系统》I2C总线(二)
  10. 微型计算机在温室管理中的应用初探,文献综述-测控051-陈杰.doc