当前位置: 首页 > 编程日记 > 正文

UVa12096.The SetStack Computer

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

1391605812096The SetStack ComputerAcceptedC++0.3022014-07-21 03:43:15

The SetStack Computer

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 in mathemat ics concerning the existence of mathematical objects
(such as numbers or functions) and their properties.
Formal versions of set theory also have a founda
tional 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 SetStack Alpha is unde
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 se
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.
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 o
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
***


 解题思路:模拟五个操作即可。读题十分重要。还有通过此题,可以增加使用set容器的熟练程度。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <stack>
 5 #include <map>
 6 #include <set>
 7 using namespace std;
 8 const int MAXN = 2010;
 9 const int N = 20;
10 
11 int cnt;
12 stack<set<int> > stk;
13 map<set<int>, int> mp;
14 set<int> s1, s2;
15 
16 void pop() {
17     s1 = stk.top();
18     stk.pop();
19     s2 = stk.top();
20     stk.pop();
21 }
22 
23 void Push() {
24     set<int> s;
25     stk.push(s);
26     printf("0\n");
27 }
28 
29 void Dup() {
30     set<int> s;
31     s = stk.top();
32     stk.push(s);
33     printf("%d\n", s.size());
34 }
35 
36 void Union() {
37     pop();
38     set<int>::iterator it;
39     for (it = s1.begin(); it != s1.end(); it++)
40         s2.insert(*it);
41     stk.push(s2);
42     printf("%d\n", s2.size());
43 }
44 
45 void Intersect() {
46     pop();
47     set<int> s3;
48     set<int>::iterator it;
49     for (it = s1.begin(); it != s1.end(); it++)
50         if (s2.find(*it) != s2.end())
51             s3.insert(*it);
52     stk.push(s3);
53     printf("%d\n", s3.size());
54 }
55 
56 void Add() {
57     pop();
58     if (s1.empty())
59         s2.insert(0);
60     else {
61         if (!mp[s1])
62             mp[s1] = cnt++;
63         s2.insert(mp[s1]);
64     }
65     stk.push(s2);
66     printf("%d\n", s2.size());
67 }
68 
69 int main() {
70     int t, n;
71     string str;
72     cin >> t;
73     while ( t --) {
74         cin >> n;
75         while (!stk.empty())
76             stk.pop();
77         cnt = MAXN;
78         mp.clear();
79         while (n--) {
80             cin >> str;
81             if (str[0] == 'P')
82                 Push();
83             else if (str[0] == 'D')
84                 Dup();
85             else if (str[0] == 'U')
86                 Union();
87             else if (str[0] == 'I')
88                 Intersect();
89             else Add();
90         }
91         printf("***\n");
92     }
93     return 0;
94 }

转载于:https://www.cnblogs.com/Destiny-Gem/p/3858134.html

相关文章:

网络设置计算机,怎么重置电脑网络设置

现如今网络已经融入了我们的生活&#xff0c;我们对网络的要求也越来越过了&#xff0c;那么你知道怎么重置电脑网络设置吗?下面是学习啦小编整理的一些关于怎么重置电脑网络设置的相关资料&#xff0c;供你参考。重置电脑网络设置的方法开始→运行→输入&#xff1a;CMD 点击…

centos 学习日记 文件默认权限:umaks

使用方法&#xff1a; [rootkin /]# umask 0022 [rootkin /]# umask -S urwx,grx,orx上面显示的是本机上面文件默认的权限。 第二个好理解。 第一个要注意的是&#xff1a; umask的分值是指"该默认值需要减掉的权限" 第一个数字可以不管他 第二&#xff0c;三&…

linux 基础命令一

linux命令基础 hash&#xff1a;hash操做 shell搜寻到的外部命令的路径结果会缓存至kv(key-value)存储中history&#xff1a;查看历史 history命令:管理命令历史。登录shell时,会读取命令历史文件中记录下的命令:~/.bash_history&#xff0c;而且新执行的命令只会记录在缓存中:…

ceph pool 相关命令

文章目录Pool创建ec pool创建副本pool创建Pool参数创建根故障域及添加osd其他命令Tier相关Pool创建 ec pool创建 创建profile ceph osd erasure-code-profile set $profile_name k$k m$m crush-failure-domainhost crush-root$group_name 创建规则 ceph osd crush rule creat…

临平职高计算机专业高职考大学,临平职高高考再传捷报 本科连续四年蝉联杭州市第一...

又到一年放榜时&#xff0c;几家欢喜几家愁。然而&#xff0c;2018年的高考成绩出来后&#xff0c;可把临平市职业高级中学(以下简称“临平职高”)的师生们乐坏了。正所谓三年寒窗&#xff0c;开出芬芳&#xff1b;三年磨剑&#xff0c;努力未变&#xff1b;三年坚守&#xff0…

音频编辑大师 3.3 注冊名 注冊码

username&#xff1a;cae3_user000注冊码&#xff1a;beslbFVpFEhxvxA0F23xW7heAeWoWjuWhvBIMN0Je1o我试过了&#xff0c;绝对能够用。转载于:https://www.cnblogs.com/mfrbuaa/p/3858221.html

兰戈 —— Rango

2019独角兽企业重金招聘Python工程师标准>>> 一部西部卡通片&#xff0c;据说恶搞了《正午》这部著名的西部片&#xff0c;可惜我没有看过《正午》。非常喜欢这部片子里的音乐&#xff0c;恢宏大气。 剧情&#xff1a; 兰戈&#xff08;约翰尼德普 Johnny Depp 配…

C#/.Net判断是否为周末/节假日

判断节假日请求的Api&#xff1a;http://tool.bitefu.net/jiari/ /// <summary>/// 判断是不是周末/节假日/// </summary>/// <param name"date">日期</param>/// <returns>周末和节假日返回true&#xff0c;工作日返回false</retu…

ceph 部署单机集群

文章目录ceph-deploy部署集群ceph-deploy 部署单机ceph-deploy 创建osdceph osd创建资源池ceph创建rbd块设备ceph创建fs文件系统本文档主要参考ceph官方命令进行部署&#xff0c;使用的时侯ceph-deploy原生命令方式进行集群各个组件的创建&#xff0c;删除&#xff0c;后续会增…

hdu-4302-Holedox Eating-线段树-单点更新,有策略的单点查询

一開始实在是不知道怎么做&#xff0c;后来经过指导&#xff0c;猛然发现&#xff0c;仅仅须要记录某个区间内是否有值就可以。 flag[i]:代表i区间内&#xff0c;共同拥有的蛋糕数量。 放置蛋糕的时候非常好操作&#xff0c;单点更新。 ip&#xff1a;老鼠当前的位置 寻找吃哪一…

华南理工计算机基础知识题,华南理工_计算机应用基础_随堂练习答案(2017年)

华南理工_计算机应用基础_随堂练习答案(2017年) (18页)本资源提供全文预览&#xff0c;点击全文预览即可全文预览,如果喜欢文档就下载吧&#xff0c;查找使用更方便哦&#xff01;19.9 积分&#xfeff;. . . .华南理工-计算机应用基础-随堂练习答案(2017年)第一章 计算机基础知…

python 添加进度条

安装&#xff1a; pip install tqdm使用&#xff1a; from tqdm import tqdm import time for i in tqdm(rang(10)):time.sleep(0.1)转载于:https://www.cnblogs.com/royfans/p/10271496.html

ceph osd 相关命令

混合osd的部署 先部署所有的ssd 在/etc/ceph.conf中最后添加ssd做osd的block大小如下&#xff1a; 比如部署中有两个ssd&#xff0c;则添加 [osd.0] bluestore_block_size xxxx [osd.1] bluestore_block_size xxx 如上的size大小计算如下&#xff0c;如ssd容量为800G&#x…

一万年太久,只争朝夕

好久没有写了&#xff0c;很多东西都已经忘记&#xff0c;不是因为别的&#xff0c;仅仅是觉得经历太多&#xff0c;没有地方装载那么多&#xff0c;想想以前的愿望&#xff0c;想过要当作家、想过要开个小店&#xff0c;但是看看现在&#xff0c;一切都变得遥不可及&#xff0…

上海职称英语和计算机考试时间,上海职称英语考试时间

上海2015年职称英语考试时间为12月25日到2015年1月15日&#xff0c;报名网站为&#xff1a;上海职业能力考试院。2015年如何短时间攻破职称英语考试关键点一&#xff1a;调整好备考心态&#xff0c;树立信心&#xff0c;切记懂乱、随便放弃总的来说&#xff0c;职称英语考生以中…

Caliburn.Micro 资源随时添加

Caliburn.Micro – Hello World http://buksbaum.us/2010/08/01/caliburn-micro-hello-world/ http://blog.csdn.net/xbgzs2010/article/details/18447625 转载于:https://www.cnblogs.com/ifendou/p/3870256.html

ros-kinetic install error: sudo rosdep init ImportError: No module named 'rosdep2'

refer to: https://blog.csdn.net/yueyueniaolzp/article/details/85070093 方法一 将Ubuntu默认python版本设置为2.7方法二 终端输入命令sudo apt-get install python3-rosdep转载于:https://www.cnblogs.com/xbit/p/10275218.html

Android:项目关联Library

为什么80%的码农都做不了架构师&#xff1f;>>> 近日&#xff0c;在做一个人人的第三方小项目。打算直接使用renren 的sdk 进行开发。因为renren的sdk是以android library project 形式发布的&#xff08;关于这种project的内容可以参考android library project&…

winxp运行html代码,关于WinXP系统实现自动化运行的操作技巧

关于WinXP系统实现自动化运行的操作技巧发布时间&#xff1a;2014-06-16 10:00:29 作者&#xff1a;佚名 我要评论与其他系统相比&#xff0c;WinXP系统的自动化运行已经大大改进&#xff0c;根据经验为大家总结了一份关于实现自动化运行的操作技巧&#xff0c;希望对大家…

ACM1881 01背包问题应用

01背包问题动态规划应用 acm1881毕业bg 将必须离开的时间限制看作背包容量&#xff0c;先将他们由小到大排序,然后在排完序的数组中对每个实例都从它的时间限制开始&#xff08;背包容量&#xff09;到它的延长时间进行遍历&#xff1b; 1 #include<iostream>2 #include&…

解决MVC返回Json中日期格式问题

问题&#xff1a;MVC中使用控制器返回JsonResult&#xff0c;如果带有日期字段的对象&#xff0c;浏览器接收到的json中会变成形如/Date(123123123)/格式。如何在easyui等中直接使用是个麻烦事。 解决方法&#xff1a;从源头开始。既然Controller控制器的Json()方法会自动转化&…

eclipse 出现user operation is waiting

project->properties->Builders 将带有 validator的选项全部去掉&#xff0c;然后保存一切就ok了。 转载于:https://www.cnblogs.com/fengnan/p/10276162.html

SHELL 技能树(持续更新)

相关xmind的原始文件已上传至mind-Mapping github,如有需要可自行下载&#xff0c;欢迎批评指正。 关于分布式存储的整体技能的学习历程 可以参考&#xff0c;分布式存储技能图谱

计算机网络 关于网速,关于电脑网速慢的说明

近期接到一些老师反馈&#xff0c;现在上网网速不如以前体验效果好。现就此反馈做一下说明&#xff0c;网速感觉慢是有多方面的原因的&#xff0c;和每个人的电脑环境有很大关系&#xff0c;比如有些终端上装有360、电脑管家之类的流氓程序的话&#xff0c;对终端的影响就很大&…

7月份没啥写的。。。

一整个月没啥写的&#xff0c;代表我啥也没学会啊。。。 没进步啊。。。 光听盗墓笔记的有声小说了。。。 我不对啊。。。我有罪。。。 我不好。。。我检讨。。。 赶紧听完&#xff0c;努力起来吧。。。 |||转载于:https://www.cnblogs.com/hydor/p/3873699.html

输入空格hdu - 1010 - Tempter of the Bone

时间紧张&#xff0c;先记一笔&#xff0c;后续优化与完善。 题意&#xff1a;一个N*M的地图&#xff0c;走过的点不能再走&#xff0c;X为墙弗成走&#xff0c;能否从点S到点D恰好用时T。&#xff08;1 < N, M < 7; 0 < T < 50&#xff09; 标题链接&#xff1a;h…

vue通信、传值

一、通过路由带参数进行传值 ①两个组件 A和B,A组件通过query把orderId传递给B组件&#xff08;触发事件可以是点击事件、钩子函数等&#xff09;this.$router.push({ path: /conponentsB, query: { orderId: 123 } }) // 跳转到B②在B组件中获取A组件传递过来的参数this.$rout…

C++ 技能树(持续更新)

相关xmind的原始文件已上传至mind-Mapping github,如有需要可自行下载&#xff0c;欢迎批评指正 关于分布式存储的整体技能的学习历程 可以参考分布式存储技能图谱&#xff0c;仅为个人的技能学习框架

(转)小小的研究了一下linux下的”注册表“ gconf-editor

最近学习linux&#xff0c;刚上手gedit&#xff0c;首先要解决的一定是编码的问题&#xff0c;总结一下方法&#xff0c;思路有下&#xff1a; 一.用图形化界面设置的方法 运行gconf-editor&#xff0c;在弹出的对话框中选择&#xff1a;/apps/gedit-2/preferences/encodings/a…