题意:

有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. bind:Address alreasy is use

    在bind邦定时,通常会出现bind:Address alreasy is use错误. 此错误可以用setsockopt函数避免 int setsockopt(int sockfd,int leve ...

  2. jacoco 的使用及与jenkins的集成

    1.把jacocoagent.jar的包放入到dockerfile COPY jacocoagent.jar /opt/jacoco/lib/jacocoagent.jar 2.打完镜像,需要启动容器 ...

  3. [PKUWC2018]猎人杀

    题解 感觉是一道神题,想不出来 问最后\(1\)号猎人存活的概率 发现根本没法记录状态 每次转移的分母也都不一样 可以考虑这样一件事情: 如果一个人被打中了 那么不急于从所有人中将ta删除,而是给ta ...

  4. C#DataTable学习心得[转]

    一.DataSet.DataTable.DataRow.DataColumn 1] 在DataSet中添加DataTable DataSet.Tables.Add(DataTable) 实例: Dat ...

  5. Vue自定义过滤器格式化数字三位加一逗号

    <template> <div class="index-compont"> <div class="totalCount"> ...

  6. IOS应用开发版本控制工具之Versions使用,iosversions

    Versions版本控制工具破解版(Versions.zip)下载请见本博文附件.下载后在MAC安装完以后,图标是莲花状.见下图: 双击运行如下图:    点击Repository,连接SVN服务器R ...

  7. Volley的初步了解

    Volley的介绍 Volley是什么? 2013年Google I/O大会上推出的网络请求和图片加载框架 其优点是api简单,性能优秀 非常适合数据量不大但是通信频繁的网络请求,而对于大数据量的操作 ...

  8. ES之值类型以及堆和栈

    ES的数据类型: 原始类型(值存在栈内存中): Number.String Boolean.undefined.null charAt(index)返回该index所在的字节,charCodeAt(i ...

  9. 2019最新版Devops工具集

    啥是Devops? 简单地来说,就是开发(DEV).测试(QA).运维(OPS)的流程,开发运维一体化,通过高度自动化工具与流程来使得软件构建.测试.发布更加快捷.频繁和可靠. 当然现实的DevOps ...

  10. 机器学习-随机梯度下降(Stochastic gradient descent)和 批量梯度下降(Batch gradient descent )

    梯度下降(GD)是最小化风险函数.损失函数的一种常用方法,随机梯度下降和批量梯度下降是两种迭代求解思路,下面从公式和实现的角度对两者进行分析,如有哪个方面写的不对,希望网友纠正. 下面的h(x)是要拟 ...