题意:

有5种操作:

PUSH:加入“{}”空集合入栈。

DUP:栈顶元素再入栈。

UNION:出栈两个集合,取并集入栈。

INTERSECT:出栈两个集合,取交集入栈。

ADD:出栈两个集合,将先出栈的加入到后出栈的集合中。

输入不超过2000, 保证操作顺利进行。

分析:

用set<int>(本身又可以映射成一个int)去模拟集合,所以可以将不同的集合映射成int型。

用一个Map<set<int>,int> 去映射成不同的int。

以后需要set做交集并集的时候再从map中拿出set做操作再映射,有点复杂,需要慢慢体会这种映射的方法。

代码有两个难点:

1.插入迭代器:

http://www.cnblogs.com/Jadon97/p/6884067.html

2.set_union() , set_intersection.

首先元素在内部要排好序,set显然满足这一点。

代码参考刘汝佳第五章例题

 #include <iostream>
#include <cstring>
#include <cstdio>
#include <set>
#include <vector>
#include <string>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <sstream> #define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
using namespace std;
typedef set<int> Set;
map<Set, int> IDcache;
vector<Set> Setcache;
int ID (Set x)
{
if(IDcache.count(x)) return IDcache[x];
Setcache.push_back(x);
return IDcache[x] = Setcache.size() - ; //都是空集, 唯一区别就是大小
}
int main()
{
// freopen("1.txt","r",stdin);
int t;
cin>>t;
while(t--)
{
IDcache.clear();
Setcache.clear();
stack<int> s;
int n;
cin>>n;
for(int i = ; i < n ; i++)
{
string op;
cin >> op;
if(op[] == 'P')
s.push(ID(Set()));
else if(op[] == 'D')
s.push(s.top());
else
{
Set x1 = Setcache[s.top()];
s.pop();
Set x2 = Setcache[s.top()];
s.pop();
Set x;
if(op[] == 'U') set_union(ALL(x1),ALL(x2),INS(x));
if(op[] == 'I') set_intersection(ALL(x1), ALL(x2), INS(x));
if(op[] == 'A')
{
x=x2;
x.insert(ID(x1));
}
s.push(ID(x));
}
cout<< Setcache[s.top()].size() <<endl;
}
cout<<"***"<<endl;
}
}

uva 12096 The SetStack Computer(STL set的各种库函数 交集 并集 插入迭代器)的更多相关文章

  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(集合栈)

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

  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. EOJ 1641/UVa The SetStack Computer

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

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

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

  9. UVA12096 - The SetStack Computer(set + map映射)

    UVA12096 - The SetStack Computer(set + map映射) 题目链接 题目大意:有五个动作: push : 把一个空集合{}放到栈顶. dup : 把栈顶的集合取出来, ...

随机推荐

  1. Vue父子组件传值之——访问根组件$root、$parent、$children和$refs

    Vue组件传值除了prop和$emit,我们还可以直接获取组件对象: 根组件: $root // 单一对象 表示当前组件树的根 Vue 实例,即new Vue({...根组件内容}).如果当前实例没有 ...

  2. Lightoj 1090 - Trailing Zeroes (II)

    题目连接: http://www.lightoj.com/volume_showproblem.php?problem=1090 题目大意: 给出n,r,p,q四个数字1<=n,r,p,q< ...

  3. 光盘安装ubuntu出现busybox-initramfs不能继续安装的终极解决方法

    我用的是Intel G45的主板,可能是板子太新的原因,kernel支持有问题 从光盘(官方寄来的光盘)安装ubuntu,出现提示: BusyBox V1.1.3 (Debian 1:1.1.3-5u ...

  4. java的构造方法 this 重载

    this1.隐含的局部变量在方法中指向调用该方法的对象()使用:当成员变量与局部变量同名的时候,通过this说明哪一个是成员变量.(this指向的是成员变量) 2.作为当前类的构造方法名存在作用:在构 ...

  5. 转-iOS开发系列--地图与定位

    来自: http://www.cnblogs.com/kenshincui/p/4125570.html#autoid-3-4-0 概览 现在很多社交.电商.团购应用都引入了地图和定位功能,似乎地图功 ...

  6. vue采坑及较好的文章汇总

    1:父子组件传动态传值 https://www.cnblogs.com/daiwenru/p/6694530.html  -----互传数据基本流程 https://blog.csdn.net/qq_ ...

  7. AJPFX关于TreeSet集合的介绍

    需求:键盘录入5个学生信息(姓名,语文成绩,数学成绩,英语成绩),按照总分从高到低输出到控制台.分析:1.创建键盘录入对象:          2.创建TreeSet集合,使用匿名内部类实现Compa ...

  8. Glide清除缓存

    Glide是谷歌推荐的一款加载图片的第三方框架,对内存优化更好,更省资源,他的众多好处,我就不一一描述了,有兴趣的朋友可以百度一下,介绍的还是挺详细的. 今天主要给大家介绍一下关于怎么获取Glide的 ...

  9. filter 过滤器加载流程

    过滤器例子 <!--A过滤器--><filter> <filter-name>mdamptRightLimitFilter</filter-name> ...

  10. VMware虚拟机下载与安装

    VMware下载与安装 一.虚拟机的下载 1.进入VMware官网,点击左侧导航栏中的下载,再点击图中标记的Workstation Pro,如下图所示. 2.根据操作系统选择合适的产品,在这里以Win ...