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的更多相关文章

  1. UVA.12096 The SetStack Computer ( 好题 栈 STL混合应用)

    UVA.12096 The SetStack Computer ( 好题 栈 STL混合应用) 题意分析 绝对的好题. 先说做完此题的收获: 1.对数据结构又有了宏观的上的认识; 2.熟悉了常用STL ...

  2. uva 12096 The SetStack Computer

    点击打开链接uva 12096 思路: STL模拟 分析: 1 题目给定5种操作,每次输出栈顶集合的元素的个数 2 利用stack和set来模拟,set保存集合的元素.遇到push的时候直接在stac ...

  3. uva 12096 - The SetStack Computer(集合栈)

    例题5-5 集合栈计算机(The Set Stack Computer,ACM/ICPC NWERC 2006,UVa12096) 有一个专门为了集合运算而设计的"集合栈"计算机. ...

  4. UVa 12096 The SetStack Computer【STL】

    题意:给出一个空的栈,支持集合的操作,求每次操作后,栈顶集合的元素个数 从紫书给的例子 A={{},{{}}} B={{},{{{}}}} A是栈顶元素,A是一个集合,同时作为一个集合的A,它自身里面 ...

  5. The SetStack Computer UVA - 12096

    题意:初始状态的栈内包含一个空集,对栈进行一下操作: PUSH:向栈内压入一个空集 DUP:复制栈顶,并压入栈内 UNION:将栈顶端两个集合出栈,并将两个元素的并集入栈 INTERSECT:将栈顶端 ...

  6. set有关的函数的用法(The SetStack Computer UVA - 12096)

    #include<bits/stdc++.h> using namespace std; typedef set<int> Set; map<Set,int> ID ...

  7. uva 12096 The SetStack Computer(STL set的各种库函数 交集 并集 插入迭代器)

    题意: 有5种操作: PUSH:加入“{}”空集合入栈. DUP:栈顶元素再入栈. UNION:出栈两个集合,取并集入栈. INTERSECT:出栈两个集合,取交集入栈. ADD:出栈两个集合,将先出 ...

  8. UVa12096.The SetStack Computer

    题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  9. EOJ 1641/UVa The SetStack Computer

    Background from Wikipedia: “Set theory is a branch of mathematics created principally by the German ...

随机推荐

  1. 判断线段和直线相交 POJ 3304

    // 判断线段和直线相交 POJ 3304 // 思路: // 如果存在一条直线和所有线段相交,那么平移该直线一定可以经过线段上任意两个点,并且和所有线段相交. #include <cstdio ...

  2. RTMP、RTSP、HTTP视频协议详解(转)

    一.RTMP.RTSP.HTTP协议 这三个协议都属于互联网 TCP/IP 五层体系结构中应用层的协议.理论上这三种都可以用来做视频直播或点播.但通常来说,直播一般用 RTMP.RTSP.而点播用 H ...

  3. 【C++专题】static_cast, dynamic_cast, const_cast探讨

    首先回顾一下C++类型转换: C++类型转换分为:隐式类型转换和显式类型转换 第1部分. 隐式类型转换 又称为“标准转换”,包括以下几种情况:1) 算术转换(Arithmetic conversion ...

  4. 详解Java解析XML的四种方法

    XML现在已经成为一种通用的数据交换格式,它的平台无关性,语言无关性,系统无关性,给数据集成与交互带来了极大的方便.对于XML本身的语法知识与技术细节,需要阅读相关的技术文献,这里面包括的内容有DOM ...

  5. python crawler0723.py

    #!/usr/env  python #-*- coding: utf-8  -*- import urllib import urllib2 import random import request ...

  6. Enterprise Library 服务问题

    在使用Enterprise Library而没有注册服务的时候会出现这样的问题,"Editing Post "Failed to create instances of perfo ...

  7. Date、String、Calendar、Timestamp类型之间的转化

    1.Calendar 转化 String Calendar calendat = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDa ...

  8. 关于php一句话免杀的分析<转载>

    一开始想这样:   <?php $_GET['ts7']($_POST['cmd']);?> 客户端用菜刀,密码cmd,url为test.php?ts7=assert   这个应该算没有什 ...

  9. Oracle DataGuard搭建(一)

    第一次搭建oracle dataguard.学oracle很长时间,却没有完整的搭过dg,说起来让人笑.总得有第一次,而且第一次总是很痛苦的. 数据库版本: Oracle Database 11g E ...

  10. 读取AD模拟分量

    //EEPROM数据保存---------------------- #include <EEPROM.h> #define EEPROM_write(address, p) {int i ...