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 ...
随机推荐
- 恒天云技术分享系列5 – 虚拟化平台性能对比(KVM & VMware)
恒天云技术分享系列:http://www.hengtianyun.com/download-show-id-14.html 概述 本性能测试报告将详细陈述各虚拟化平台基准性能测试的主要结论和详细结果. ...
- 更换Oracle备份数据文件
应用背景:需要查看和修改一下Interlib中的数据,所以要反复的将备份数据进行导入和清空.整理一下步骤 删除tablespace drop tablespace interlib including ...
- The h.264 Sequence Parameter Set
转债: http://www.cardinalpeak.com/blog/the-h-264-sequence-parameter-set/ View from the Peak The h.264 ...
- Salt自动化之自动更新Gitfs-爱折腾技术网
Salt自动化之自动更新Gitfs-爱折腾技术网 pygit2
- Android问题-DelphiXE8新建AVD出现“no system images installed for this target”
相关资料: 1.http://www.cnblogs.com/yc-755909659/p/4080645.html 问题现象:创建Android模拟器提不”no system images inst ...
- UVA 11300 Spreading the Wealth
题目大意:n个人手中有些金币,每个人可给相邻两个人一些金币,使得最终每个人手中金币数相同,求被转手的金币最少数 m为最终每个人手中的金币数,a1,a2,a3,...,an为每个人开始时手中的金币数,x ...
- Linux下c/c++项目代码覆盖率的产生方法
最近做了一系列的单元测试相关的工作,除了各种规范及测试框架以外,讨论比较多的就是关于代码覆盖率的产生,c/c++与其他的一些高级语言或者脚本语言相比较而言,例如 Java..Net和php/pytho ...
- Educational Codeforces Round 1(C. Nearest vectors)
题目链接:http://codeforces.com/problemset/problem/598/C 题意是给你一个数n,下面n行,每行给你横坐标x和纵坐标y(x != 0 && y ...
- 在ASP.NET MVC中的四大筛选器(Filter)及验证实现
http://www.cnblogs.com/artech/archive/2012/08/06/action-filter.html http://www.cnblogs.com/ghhlyy/ar ...
- JavaEE通过response实现请求重定向
请求重定向指的是一个web资源收到客户端请求后,通知客户端去访问另外一个web资源,这称之为请求重定向.302状态码和location头即可实现重定向. 请求重定向最常见的应用场景就是用户登录. 下面 ...