例题5-5

集合栈计算机(The

Set

Stack

Computer,ACM/ICPC

NWERC

2006,UVa12096)

有一个专门为了集合运算而设计的“集合栈”计算机。该机器有一个初始为空的栈,并且

支持以下操作。

PUSH:空集“{}”入栈。

DUP:把当前栈顶元素复制一份后再入栈。

UNION:出栈两个集合,然后把二者的并集入栈。

INTERSECT:出栈两个集合,然后把二者的交集入栈。ADD:出栈两个集合,然后把先出栈的集合加入到后出栈的集合中,把结果入栈。

每次操作后,输出栈顶集合的大小(即元素个数)。例如,栈顶元素是A={{},

{{}}},下一个元素是B={{},{{{}}}},则:

UNION操作将得到{{},{{}},{{{}}}},输出3。

INTERSECT操作将得到{{}},输出1。

ADD操作将得到{{},{{{}}},{{},{{}}}},输出3。

输入不超过2000个操作,并且保证操作均能顺利进行(不需要对空栈执行出栈操作)。

Sample Input

2

9

PUSH

DUP

ADD

PUSH

ADD

DUP

ADD

DUP

UNION

5

PUSH

PUSH

ADD

PUSH

INTERSECT

Sample Output

0

0

1

0

1

1

2

2

2

***

0

0

1

0

0

***

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=835&problem=3248&mosmsg=Submission+received+with+ID+21273791

性质:

1、set< int >  代表是集合

2、idcache 是集合与其编号的映射,每一个集合都有唯一的编号。

3、setcache是向量,即下标与其存的是集合的元素相映射。

4、每一个集合都只在以上容器中存在一个。

5、栈里存的只是集合的编号。

#include <iostream>
#include <set>
#include <map>
#include <stack>
#include <vector>
using namespace std; map< set<int> ,int> idcache; //每一个集合对应一个编号
vector< set<int> > setcache; //每一个编号对应一个集合 int getid(set<int> s)
{
if(idcache.count(s))
return idcache[s];
else
setcache.push_back(s); //将集合存入vector
idcache[s]=setcache.size()-1; //将集合存入map,从0开始,为了让map中的键与值与vector元素与下标相对应
return idcache[s];
} int main()
{
int T;
cin>>T;
while(T--)
{
stack<int> s; //只是存的集合的编号
idcache.clear();
setcache.clear();
int n;
cin>>n;
while(n--)
{ // cout<<"n"<<n<<endl;
string str;
cin>>str;
if(str=="PUSH")
{
set<int> temps;
int temp=getid(temps);
s.push(temp);
}
else if(str=="DUP")
{
s.push(s.top());
}
else
{
set<int> first =setcache[s.top()]; //这时要取的是vetor中的元素,是set<int>类型,而s里元素是int类型
s.pop();
set<int> second=setcache[s.top()];
s.pop();
set<int> ans;
if(str=="INTERSECT")
{
set<int>::iterator i=first.begin();
for(;i!=first.end();i++)
{
set<int>::iterator j=second.begin();
for(;j!=second.end();j++)
{
if(*i==*j)
ans.insert(*i);
}
}
}
if(str=="UNION")
{
ans=second; //直接赋值set<int>
set<int>::iterator i=first.begin();
for(;i!=first.end();i++)
{
ans.insert(*i);
}
}
if(str=="ADD")
{
ans=second; //直接赋值set<int>
ans.insert(getid(first)); //插入int型,是first对应的编号,将first作为了ans的一个元素
}
s.push(getid(ans));
}
cout<<setcache[s.top()].size()<<endl; // cout<<str<<":"<<endl;
// stack<int> t=s;
// for(int i=0;i<s.size();i++)
// { int ii=t.top();
// t.pop();
// set<int>::iterator it=setcache[ii].begin();
// for(;it!=setcache[ii].end();it++)
// cout<< *it<<" ";
// cout<<"|"<<endl;
// }
// cout<<endl; }
cout<<"***"<<endl;
}
return 0;
}
//AC at 2018/5/7

uva 12096 - The SetStack Computer(集合栈)的更多相关文章

  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【STL】

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

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

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

  5. 12096 - The SetStack Computer UVA

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

  6. UVa12096.The SetStack Computer

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

  7. 集合栈计算机(The SetStack Computer, ACM/ICPC NWERC 2006,Uva12096)

    集合栈计算机(The SetStack Computer, ACM/ICPC NWERC 2006,Uva12096) 题目描述 有一个专门为了集合运算而设计的"集合栈"计算机.该 ...

  8. UVa 12096 (STL) The SetStack Computer

    题意: 有一个集合栈计算机,栈中的元素全部是集合,还有一些相关的操作.输出每次操作后栈顶集合元素的个数. 分析: 这个题感觉有点抽象,集合还能套集合,倒是和题中配的套娃那个图很贴切. 把集合映射成ID ...

  9. UVa - 12096 集合栈计算机(STL)

    [题意] 有一个专门为了集合运算而设计的“集合栈”计算机.该机器有一个初始为空的栈,并且支持以下操作:PUSH:空集“{}”入栈DUP:把当前栈顶元素复制一份后再入栈UNION:出栈两个集合,然后把两 ...

随机推荐

  1. Hive的运算和函数大全

    hive 常用运算 测试各种内置函数的快捷方法: 创建一个 dual 表 create table dual(id string); load 一个文件(只有一行内容:内容为一个空格)到 dual 表 ...

  2. Servlet 2.5为cookie配置HTTPOnly属性

    cookie的HTTPOnly属性,主要是用来防止JavaScript来读取cookie,默认情况下,JavaScript可以通过document.cookie来读取cookie,这样是很不安全的.通 ...

  3. apache-实战(一)

     Apache 1.html的完整格式 # vim /var/www/html/index.html<html><head><title>我要</title& ...

  4. February 23 2017 Week 8 Thursday

    In order to be irreplaceable, one must always be different. 想要无可取代,必须与众不同. In recent days, a news ab ...

  5. js原型链继承及调用父类方法

    方法1: var Parent= function () { }; Parent.prototype.process = function(){ alert('parent method'); }; ...

  6. BZOJ2730:[HNOI2012]矿场搭建(双连通分量)

    Description 煤矿工地可以看成是由隧道连接挖煤点组成的无向图.为安全起见,希望在工地发生事故时所有挖煤点的工人都能有一条出路逃到救援出口处.于是矿主决定在某些挖煤点设立救援出口,使得无论哪一 ...

  7. POJ 1986 Distance Queries 【输入YY && LCA(Tarjan离线)】

    任意门:http://poj.org/problem?id=1986 Distance Queries Time Limit: 2000MS   Memory Limit: 30000K Total ...

  8. JS动态修改微信浏览器中的title

    JS动态修改微信浏览器中的title我们的原理是设置一个ifame然后我们再加载一下就可以实现了,具体的例子如下所示. 平时使用JS修改title,直接document.title=新标题就好了 这样 ...

  9. maven学习记录二——依赖管理

    5       依赖管理 Jar包的管理 需求:整合struts2   页面上传一个客户id 跳转页面 5.1     添加依赖: 打开maven仓库的视图: 5.2     重建索引 1.  创建m ...

  10. 【luogu P1186 玛丽卡】 题解

    题目链接:https://www.luogu.org/problemnew/show/P1186 邻接表开大开小真的奇妙,毒瘤玩意,再您妈的见. #include <queue> #inc ...