题目链接: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. 今天弄了整整一天DCloud

    开发一个新闻移动客户端 起先用appcan.apiCloud,最后选择了DCloud: MUI文档看了n久,js代码一上来不很适应编码风格,一堆括号弄得头大: 数据库由爬虫程序将新闻页面数据爬出来: ...

  2. NUnit - 使用感受

    Nunit使用 最近项目开始大量使用Nunit, 发现Nunit还是有很多好处的. 1. 测试驱动逻辑,这样可以尽最大可能减少“修改”引入的Bug. 如果你修改了一些东西,导致Case跑不过.请检查你 ...

  3. Java:单例模式的七种写法[转]

    第一种(懒汉,线程不安全):  1 public class Singleton {   2     private static Singleton instance;   3     privat ...

  4. ThinkPHP3.2.3中三大自动中的缺陷问题

    我们在使用Thinkphp3.2.3框架时在对数据表进行模型化后就可以使用自动完成功能. 自动完成可以帮助我们更简便的完成对表单内容对数据表(集合)的填充,自动完成是基于: 当实例化数据库user后, ...

  5. Android 按二次后退键退出应用程序

    前言          欢迎大家我分享和推荐好用的代码段~~ 声明          欢迎转载,但请保留文章原始出处:          CSDN:http://www.csdn.net        ...

  6. [Javascript] Call Stack

    Every time when a function run it will be push into the call stack and put on the top,  you can thin ...

  7. JSP实现分页功能

    分页须知知识点: (1)JDBC2.0的可滚动结果集. (2)HTTP GET请求. 一.可滚动结果集   Connection con  = DriverManager.getConnection( ...

  8. 如何实现带照片缩略图的Listview

    ackage com.demo; import java.util.ArrayList; import android.app.Activity; import android.content.Con ...

  9. Web App 前端构建(纯净版)

    asp.net 母版页: <!DOCTYPE html> <html> <head> <meta charset="utf-8" name ...

  10. SQL获取前一天0:00:00至23:59:59数据

    一.前言 因为我公司要做财务结算前一天0:00:00至23:59:59的数据,利用到动态拼接SQL语句 我们需要明白声明DateTime 和 Date 获取的时间格式是不一样的,所以通过此计算有利于得 ...