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 ...
随机推荐
- 2.1……Android中的单位简介
引用自Google API Guides Dimension A dimension value defined in XML. A dimension is specified with a num ...
- php里面为什么header之前有输出报错 源码分析
众所周知,php 里面 header之前有输出的话,会报错,例如下面这样 就这个错误,我们开始查阅php源代码,到底是怎样做的,至于php源代码分析,安装,和调试时怎样配置的,我会专门写一篇文章去 ...
- SQL中以count及sum为条件的查询
在开发时,我们经常会遇到以“累计(count)”或是“累加(sum)”为条件的查询.比如user_num表: id user num 1 a 3 2 a 4 3 b 5 4 b 7 例1:查询出现 ...
- [Hive - LanguageManual ] ]SQL Standard Based Hive Authorization
Status of Hive Authorization before Hive 0.13 SQL Standards Based Hive Authorization (New in Hive 0. ...
- The Services(服务)
datastore和运行时环境的关系就是和一个服务的关系:应用使用API访问一个独立的系统(separate system),这个系统管理应用的所有的独立于应用实例的扩展需求(scaling need ...
- LUA和C++绑定的一些天然的麻烦
最近在看Luatinker的源代码,打算自己改(仿写)写搞一个简单的封装C++和LUA的结合的库,结果发现其实麻烦和困惑比想象的多. 比如这些点: 1)有时候使用模板的时候,引用会退化. classt ...
- [转]解决百度统计 gzdecode(): insufficient memory
百度统计API gzdecode($preLogin->retData, strlen($preLogin->retData)) 这段代码会造成一个PHP警告内存不足,解决办法只要换个解压 ...
- STM32的GPIO使用的函数剖析
转载http://blog.csdn.net/wuwuhuizheyisheng/article/details/8239599 STM32的GPIO总结 作者:JCY 该文是自己学习了一段STM32 ...
- picture to string
图片转化为字符原理: 一张m*n大小的图片,实际上可以看成是一个m*n的矩阵.矩阵的每一个元素就是一个Color值,不同的Color值,用不同的Ascii可以在屏幕上打印显示的字符来代替,于是可以得到 ...
- hdu 5745 La Vie en rose DP + bitset优化
http://acm.hdu.edu.cn/showproblem.php?pid=5745 这题好劲爆啊.dp容易想,但是要bitset优化,就想不到了. 先放一个tle的dp.复杂度O(n * m ...