uva 12096 - The SetStack Computer(集合栈)
例题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
***
性质:
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(集合栈)的更多相关文章
- 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【STL】
题意:给出一个空的栈,支持集合的操作,求每次操作后,栈顶集合的元素个数 从紫书给的例子 A={{},{{}}} B={{},{{{}}}} A是栈顶元素,A是一个集合,同时作为一个集合的A,它自身里面 ...
- uva 12096 The SetStack Computer(STL set的各种库函数 交集 并集 插入迭代器)
题意: 有5种操作: PUSH:加入“{}”空集合入栈. DUP:栈顶元素再入栈. UNION:出栈两个集合,取并集入栈. INTERSECT:出栈两个集合,取交集入栈. ADD:出栈两个集合,将先出 ...
- 12096 - The SetStack Computer UVA
Background from Wikipedia: \Set theory is a branch of mathematics created principally by the German ...
- UVa12096.The SetStack Computer
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- 集合栈计算机(The SetStack Computer, ACM/ICPC NWERC 2006,Uva12096)
集合栈计算机(The SetStack Computer, ACM/ICPC NWERC 2006,Uva12096) 题目描述 有一个专门为了集合运算而设计的"集合栈"计算机.该 ...
- UVa 12096 (STL) The SetStack Computer
题意: 有一个集合栈计算机,栈中的元素全部是集合,还有一些相关的操作.输出每次操作后栈顶集合元素的个数. 分析: 这个题感觉有点抽象,集合还能套集合,倒是和题中配的套娃那个图很贴切. 把集合映射成ID ...
- UVa - 12096 集合栈计算机(STL)
[题意] 有一个专门为了集合运算而设计的“集合栈”计算机.该机器有一个初始为空的栈,并且支持以下操作:PUSH:空集“{}”入栈DUP:把当前栈顶元素复制一份后再入栈UNION:出栈两个集合,然后把两 ...
随机推荐
- redis缓存技术在java中的应用
https://blog.csdn.net/zhh1072773034/article/details/78538012
- 实现动态代理(Java和spring)
一.Java实现动态代理 1.创建接口 package com.oyy.mw.biz.i; public interface Cal { public int add(int num1,int num ...
- how to determint wether two column equals
question: How to determint wether two column equals? Answer: Table one:RecordId = 1,CommonId = 5,Ap ...
- 笨办法学Python(四十)
习题 40: 字典, 可爱的字典 接下来我要教你另外一种让你伤脑筋的容器型数据结构,因为一旦你学会这种容器,你将拥有超酷的能力.这是最有用的容器:字典(dictionary). Python 将这种数 ...
- Oracle transport tablespace
本来没想过发布这个文章,只是周边有一朋友工作中遇到合并数据库的情况,他是通过expdp提取出五个库对象,然后impdp到新库里面.我觉得这种方法特别耗时,尤其在数据量比较大的时候.这种时候我觉得采用表 ...
- python 用cookie模拟登陆网站
import re import requests def get_info(url): headers = { "Cookie" :"***************** ...
- C++11之 Move semantics(移动语义)(转)
转https://blog.csdn.net/wangshubo1989/article/details/49748703 按值传递的意义是什么? 当一个函数的参数按值传递时,这就会进行拷贝.当然,编 ...
- PythonTip(1)
发现一个Python的题库,嘿嘿,练练手吧~~~ http://www.pythontip.com/ a + b 描述: 给你两个数a.b,请你计算它们的和,并输出. 例如: a = 3, b = 2 ...
- 2018.10.31 Mac下的Mysql修改字符编码修改的问题总结
今天在弄数据库的时候发现存入中文汉字变成了问号,Mac跟windows处理方式不一样. show variables like '%char%'; 查看当前mysql的编码格式 也就是默认编码格式 + ...
- 初学bind
其实项目中还没有用到. 但自己还是想逐步了解一些高级的JS语法,不是为了炫技,也不像找前端的工作. 主要目的是:1.学习设计思想,提升解决问题的能力2.让自己的脑子动起来,别太笨. 简单的几句话总结一 ...