UVa12096.The SetStack Computer
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3248
| 13916058 | 12096 | The SetStack Computer | Accepted | C++ | 0.302 | 2014-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容器的熟练程度。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
#include <map>
#include <set>
using namespace std;
const int MAXN = ;
const int N = ; int cnt;
stack<set<int> > stk;
map<set<int>, int> mp;
set<int> s1, s2; void pop() {
s1 = stk.top();
stk.pop();
s2 = stk.top();
stk.pop();
} void Push() {
set<int> s;
stk.push(s);
printf("0\n");
} void Dup() {
set<int> s;
s = stk.top();
stk.push(s);
printf("%d\n", s.size());
} void Union() {
pop();
set<int>::iterator it;
for (it = s1.begin(); it != s1.end(); it++)
s2.insert(*it);
stk.push(s2);
printf("%d\n", s2.size());
} void Intersect() {
pop();
set<int> s3;
set<int>::iterator it;
for (it = s1.begin(); it != s1.end(); it++)
if (s2.find(*it) != s2.end())
s3.insert(*it);
stk.push(s3);
printf("%d\n", s3.size());
} void Add() {
pop();
if (s1.empty())
s2.insert();
else {
if (!mp[s1])
mp[s1] = cnt++;
s2.insert(mp[s1]);
}
stk.push(s2);
printf("%d\n", s2.size());
} int main() {
int t, n;
string str;
cin >> t;
while ( t --) {
cin >> n;
while (!stk.empty())
stk.pop();
cnt = MAXN;
mp.clear();
while (n--) {
cin >> str;
if (str[] == 'P')
Push();
else if (str[] == 'D')
Dup();
else if (str[] == 'U')
Union();
else if (str[] == 'I')
Intersect();
else Add();
}
printf("***\n");
}
return ;
}
UVa12096.The SetStack Computer的更多相关文章
- UVA12096 - The SetStack Computer(set + map映射)
UVA12096 - The SetStack Computer(set + map映射) 题目链接 题目大意:有五个动作: push : 把一个空集合{}放到栈顶. dup : 把栈顶的集合取出来, ...
- uva12096 The SetStack Computer By sixleaves
代码 typedef map<Set, vector<Set> Setcache; stack< ci ...
- 集合栈计算机(The SetStack Computer, ACM/ICPC NWERC 2006,Uva12096)
集合栈计算机(The SetStack Computer, ACM/ICPC NWERC 2006,Uva12096) 题目描述 有一个专门为了集合运算而设计的"集合栈"计算机.该 ...
- 12096 - The SetStack Computer UVA
Background from Wikipedia: \Set theory is a branch of mathematics created principally by the German ...
- UVA.12096 The SetStack Computer ( 好题 栈 STL混合应用)
UVA.12096 The SetStack Computer ( 好题 栈 STL混合应用) 题意分析 绝对的好题. 先说做完此题的收获: 1.对数据结构又有了宏观的上的认识; 2.熟悉了常用STL ...
- EOJ 1641/UVa The SetStack Computer
Background from Wikipedia: “Set theory is a branch of mathematics created principally by the German ...
- 集合栈计算机 (The SetStack Computer,ACM/ICPC NWERC 2006,UVa12096
题目描述: #include<iostream> #include<string> #include<set> #include<map> #inclu ...
- 【紫书】(UVa12096) The SetStack Computer
突然转进到第五章的low题目的原因是做到图论了(紫书),然后惊喜的发现第一题就做不出来.那么里面用到了这一题的思想,我们就先解决这题.当然,dp必须继续做下去,这是基本功.断不得. 题意分析 这条题真 ...
- uva 12096 - The SetStack Computer(集合栈)
例题5-5 集合栈计算机(The Set Stack Computer,ACM/ICPC NWERC 2006,UVa12096) 有一个专门为了集合运算而设计的"集合栈"计算机. ...
随机推荐
- 学习phpcms模板方法:
1.改官方模板,读里面的代码,改改它,看看有什么变化,如果不明白,去官方论 坛.查手册.专业人士还可以看数据库.2.复制实例代码,整理笔记,到实战的时候,就直接复制,改改参数即可.
- Django之CSRF 跨站请求伪造
一.简介 1.点我了解什么是跨站请求伪造 2.django为用户实现防止跨站请求伪造的功能,通过中间件 django.middleware.csrf.CsrfViewMiddleware 来完成.而对 ...
- leetcode_question_57 Insert Interval
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- c++11 生产者/消费者
下面是一个生产者消费者问题,来介绍condition_variable的用法.当线程间的共享数据发生变化的时候,可以通过condition_variable来通知其他的线程.消费者wait 直到生产者 ...
- 将宿主机东西拷贝到dokcer容器中去
1,获取容器名称或者id : docker ps 2,获取整个容器的id,其实键盘tag就可以补全的. docker inspect -f '{{.Id}}' 步骤A获取的名称或者id 3,在主机 ...
- 只包含schema的dll生成和引用方法
工作中,所有的tools里有一个project是只包含若干个schema的工程,研究了一下,发现创建这种只包含schema的dll其实非常简单. 首先,在visual studio-new proje ...
- TFS 用户与组管理(转)
作者:frank.liu kaka.zhou 安装 Team Foundation Server 后,会创建以下全局组.可以使用这些全局组来控制 Team Foundation 用户的权限. 组 权限 ...
- 16 Socket通信(简单例子)
服务端代码: import java.io.*; import java.net.ServerSocket; import java.net.Socket; import java.util.Da ...
- FFT与多项式、生成函数题目泛做
题目1 COGS 很强的乘法问题 高精度乘法用FFT加速 #include <cstdlib> #include <iostream> #include <algorit ...
- hdu 1258 DFS
I - 深搜 基础 Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:10000KB 64bi ...