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 ...
随机推荐
- 实现 Web 后端和客户端之间的分布式和认证通讯
stack.io 是一个用于实现 Web 后端和客户端之间的分布式和认证通讯. 服务器端进程之间的通讯是非常高效的,因为没有中间的代理.而来自客户端的请求通过 socket.io 进入 Node.js ...
- android 源码编译中的错误 解决
1.编译种错误提示: arm-none-linux-gnueabi-gcc: directory: No such file or directory arm-none-linux-gnueabi-g ...
- 【转】 Nginx系列(一)--nginx是什么?
原博文出于:http://blog.csdn.net/liutengteng130/article/details/46700939 感谢! 一.介绍 Nginx是一个高性能的HTTP和反向代理服务 ...
- 【转载】10个有用的du命令行
10 Useful du (Disk Usage) Commands to Find Disk Usage of Files and Directories The Linux “du” (Disk ...
- [iOS基础控件 - 6.10.3] DatePicker & UIToolBar
A.需求 1. 学习DatePicker的基本配置 2.使用TextField召唤指定类型的输入键盘View,这里使用DatePicker 3.给输入键盘上方加上一个UIToolBar,实现如关闭键盘 ...
- Educational Codeforces Round 3 E. Minimum spanning tree for each edge (最小生成树+树链剖分)
题目链接:http://codeforces.com/contest/609/problem/E 给你n个点,m条边. 问枚举每条边,问你加这条边的前提下组成生成树的权值最小的树的权值和是多少. 先求 ...
- contest7.20(暴力专练)
此次练习的地址: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=26732#overview 密码 acmore Problem A(P ...
- hibernate id生成器配置
1.uuid配置 <id name="tomdId" type="java.lang.String"> <column name=" ...
- 剑指OFFER之二维数组中的查找(九度OJ1384)
题目描述: 大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项.斐波那契数列的定义如下: 输入: 输入可能包含多个测试样例,对于每个测试案例, 输入包括一个整数n(1< ...
- 安卓任意两个或多个Fragment之间的交互与刷新界面
平时项目中遇到一个问题:在子fragment中刷新父fragment的界面,通俗的说也就是在任何一个fragment中来刷新另一个fragment.大家都知道activity和fragment之间的交 ...