题目链接: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的更多相关文章

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

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

  2. uva12096 The SetStack Computer By sixleaves

    代码     typedef  map<Set,  vector<Set> Setcache;                  stack<               ci ...

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

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

  4. 12096 - The SetStack Computer UVA

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

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

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

  6. EOJ 1641/UVa The SetStack Computer

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

  7. 集合栈计算机 (The SetStack Computer,ACM/ICPC NWERC 2006,UVa12096

    题目描述: #include<iostream> #include<string> #include<set> #include<map> #inclu ...

  8. 【紫书】(UVa12096) The SetStack Computer

    突然转进到第五章的low题目的原因是做到图论了(紫书),然后惊喜的发现第一题就做不出来.那么里面用到了这一题的思想,我们就先解决这题.当然,dp必须继续做下去,这是基本功.断不得. 题意分析 这条题真 ...

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

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

随机推荐

  1. C#使用WebKitBrowser.dll填坑记

    .Net 自带的 Webbrowser 有着太多的平台限制.对于用户体验之上的今天,这无疑是一个噩梦, 然后就开始找 .Net下的WebKitBrowser.dll (后面提供下载) 从开源网站下到程 ...

  2. delphi 简单的删除字符串尾部数字的代码

    delphi  简单的删除字符串尾部数字的代码 方式一: function FilterShowName(const sName: String): String; var I: Integer; b ...

  3. JSF和Struts的区别概述

    JSF和Struts的区别概述,都采用taglib来处理表示层:在jsp页面中,二者都是采用一套标记库来处理页面的表示和model层的交互. 据说JSF的主要负责人就是struts的主要作者,所以二者 ...

  4. hdu 3056 病毒侵袭持续中 AC自己主动机

    http://acm.hdu.edu.cn/showproblem.php?pid=3065 刘汝佳的模板真的非常好用,这道题直接过 学到: cnt数组记录单词出现次数 以及map存储单词编号与字符串 ...

  5. Linux多任务编程——线程

    线程基础 △ 由于进程的地址空间是私有的,因此在进行上下文切换时,系统开销比较大 △ 在同一个进程中创建的线程共享该进程的地址空间 △ 通常线程值得是共享相同地址空间的多个任务 △ 每个线程的私有这些 ...

  6. CSS样式之背景、文本

    一.背景     1.背景颜色用background-color属性,例如:body{background-color:red}     2.用图像做背景用background-image属性,例如b ...

  7. C#在局域网中连接Liunx上的MySql数据库

    前期准备工作: 我所用的平台是VS2010和Ubuntu 14.04.3  LTS 一.由于MySql并没有集成在VS2010中所以要先安装MySQL Connector Net 6.9.8连接工具, ...

  8. SQL cmd 实用工具学习 -1

    启动 sqlcmd 实用工具并连接到 SQL Server 的默认实例 在"开始"菜单上,单击"运行". 在"打开"框中,键入 cmd,然后 ...

  9. Oracle语句块PL/SQL循环判断

    - --pl/sql Procedural Language /sql --被数据库编译保存,由用户调用 --程序块 /* 语法 Declare – 声明变量 --声明变量 Age int; //没有 ...

  10. Visifire Chart控件设置 柱状图 条的宽窄

     Chart myChart = new Chart();myChart.DataPointWidth = 5;宽度以PlotArea的百分比为单位,如下例: chart.Width = 500; c ...