12096 - The SetStack Computer UVA
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 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
j
S
j
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 set
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
j
A
j
= and
j
B
j
= . Then: UNION
would result in the set
ffg
,
ffgg
,
fffgggg
. The output is . INTERSECT
would result in the set
ffgg
. The output is . ADD
would result in the set
ffg
,
fffggg
,
ffg
,
ffgggg
. The output is .
Input
An integer T 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 N . Then follow
N
lines each containing one of
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).
SampleInput PUSH
DUP
ADD
PUSH
ADD
DUP
ADD
DUP
UNION PUSH
PUSH
ADD
PUSH
INTERSECT
SampleOutput *** ***
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <sstream>
#include <cctype>
using namespace std;
const int INF = 0x7fffffff;
const double EXP = 1e-;
const int MS = ;
typedef long long LL;
int id;
typedef set<int> SET;
map<SET, int> mp;
typedef set<int>::iterator IT;
stack<SET> sta;
void ID(SET s)
{
if (mp.count(s))
return;
mp[s] = id++;
} void PUSH()
{
SET S;
ID(S);
sta.push(S);
} void DUP()
{
sta.push(sta.top());
} void UNION()
{
SET S, S2;
S2 = sta.top();
sta.pop();
S = sta.top();
sta.pop();
for (IT it = S2.begin(); it != S2.end(); it++)
S.insert(*it);
ID(S);
sta.push(S);
} void INTERSECT()
{
SET S, S2, S3;
S2 = sta.top();
sta.pop();
S3 = sta.top();
sta.pop();
for (IT it = S2.begin(); it != S2.end(); it++)
{
if (S3.count(*it))
S.insert(*it);
}
ID(S);
sta.push(S);
} void ADD()
{
SET S1, S2;
S1 = sta.top();
sta.pop();
S2 = sta.top();
sta.pop();
S2.insert(mp[S1]);
ID(S2);
sta.push(S2);
} void TOPSIZE()
{
cout << sta.top().size() << endl;
}
void solve()
{
char op[];
cin >> op;
switch (op[])
{
case 'P':PUSH(); break;
case 'D':DUP(); break;
case 'U':UNION(); break;
case 'I':INTERSECT(); break;
case 'A':ADD(); break;
}
TOPSIZE();
}
int main()
{
int T;
cin >> T;
while (T--)
{
int n;
cin >> n;
mp.clear();
while (!sta.empty())
sta.pop();
id = ;
while (n--)
solve();
cout << "***" << endl;
}
return ;
}
12096 - The SetStack Computer UVA的更多相关文章
- UVA.12096 The SetStack Computer ( 好题 栈 STL混合应用)
UVA.12096 The SetStack Computer ( 好题 栈 STL混合应用) 题意分析 绝对的好题. 先说做完此题的收获: 1.对数据结构又有了宏观的上的认识; 2.熟悉了常用STL ...
- uva 12096 The SetStack Computer
点击打开链接uva 12096 思路: STL模拟 分析: 1 题目给定5种操作,每次输出栈顶集合的元素的个数 2 利用stack和set来模拟,set保存集合的元素.遇到push的时候直接在stac ...
- uva 12096 - The SetStack Computer(集合栈)
例题5-5 集合栈计算机(The Set Stack Computer,ACM/ICPC NWERC 2006,UVa12096) 有一个专门为了集合运算而设计的"集合栈"计算机. ...
- UVa 12096 The SetStack Computer【STL】
题意:给出一个空的栈,支持集合的操作,求每次操作后,栈顶集合的元素个数 从紫书给的例子 A={{},{{}}} B={{},{{{}}}} A是栈顶元素,A是一个集合,同时作为一个集合的A,它自身里面 ...
- The SetStack Computer UVA - 12096
题意:初始状态的栈内包含一个空集,对栈进行一下操作: PUSH:向栈内压入一个空集 DUP:复制栈顶,并压入栈内 UNION:将栈顶端两个集合出栈,并将两个元素的并集入栈 INTERSECT:将栈顶端 ...
- set有关的函数的用法(The SetStack Computer UVA - 12096)
#include<bits/stdc++.h> using namespace std; typedef set<int> Set; map<Set,int> ID ...
- uva 12096 The SetStack Computer(STL set的各种库函数 交集 并集 插入迭代器)
题意: 有5种操作: PUSH:加入“{}”空集合入栈. DUP:栈顶元素再入栈. UNION:出栈两个集合,取并集入栈. INTERSECT:出栈两个集合,取交集入栈. ADD:出栈两个集合,将先出 ...
- UVa12096.The SetStack Computer
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- EOJ 1641/UVa The SetStack Computer
Background from Wikipedia: “Set theory is a branch of mathematics created principally by the German ...
随机推荐
- HDU 5919 -- Sequence II (主席树)
题意: 给一串数字,每个数字的位置是这个数第一次出现的位置. 每个询问对于序列的一个子区间,设一共有k个不同的数,求第ceil(k/2)个数的位置. 因为强制在线,所以离线乱搞pass掉. 主席树可解 ...
- 那些经常被遗忘的 Java 面试题
静态类和静态方法 如果一个类要被声明为static的,只有一种情况,就是静态内部类. 静态内部类实际上与普通类(即类名必须与文件名一样的顶级类)一样,只是静态内部类在某一类的内部定义了而已,既然是类, ...
- 第二百九十三天 how can I 坚持
总感觉怪怪的,换了个领导,好烦,虽然对我没用影响. 其实,还是智商低,不懂人情世故,就像...算了,不说了,只能当自己傻. 最近好冷啊,十年不遇的寒冬. 心情有些压抑. 不玩游戏了,看了集康熙来了.小 ...
- Struts2的国际化
1.概述 把在无需改写源代码即可让开发出来的应用程序能够支持多种语言和数据格式的技术称为国际化. 与国际化对应的是本地化, 指让一个具备国际化支持的应用程序支持某个特定的地区 Struts2国际化是建 ...
- JDBC学习笔记(7)——事务的隔离级别&批量处理
数据库事务的隔离级别 对于同时运行的多个事务, 当这些事务访问数据库中相同的数据时, 如果没有采取必要的隔离机制, 就会导致各种并发问题:脏读: 对于两个事务 T1, T2, T1 读取了已经被 T2 ...
- HDU 2425 DNA repair (AC自动机+DP)
DNA repair Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- linux系统中如何查看日志 (常用命令)
cat tail -f 日 志 文 件 说 明 /var/log/message 系统启动后的信息和错误日志,是Red Hat Linux中最常用的日志之一 /var/log/secure 与安全相关 ...
- HDU 5835 Danganronpa (水题)
题意:给定 n 个礼物有数量,一种是特殊的,一种是不特殊的,要分给一些人,每人一个特殊的一个不特殊,但是不特殊的不能相邻的,问最多能分给多少人. 析:是一个比较简单的题目,我们只要求差值就好,先算第一 ...
- 在MVC项目中使用RDLC报表
原文地址:http://www.cnblogs.com/wuhuacong/p/4109833.html RDLC是一个不错的报表,有着比较不错的设计模式和展现效果,在我的Winform开发里面,使用 ...
- VC中监测程序运行时间(二)-毫秒级
/* * 微秒级计时器,用来统计程序运行时间 * http://blog.csdn.net/hoya5121/article/details/3778487#comments * //整理 [10/1 ...