题意:给出一个空的栈,支持集合的操作,求每次操作后,栈顶集合的元素个数

从紫书给的例子

A={{},{{}}}

B={{},{{{}}}}

A是栈顶元素,A是一个集合,同时作为一个集合的A,它自身里面也可以集合套集合

 #include<iostream>
#include<cstdio>
#include<cstring>
#include <cmath>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<algorithm>
#define mod=1e9+7;
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
using namespace std; typedef long long LL;
typedef set<int> Set;
map<Set,int> IDcache;//每一个集合对应有一个ID编号
vector<Set> Setcache;//根据每个集合的ID编号找到对应的集合 int ID(Set x){
if(IDcache.count(x)) return IDcache[x];//查找给定集合的编号,找到了,则返回编号
Setcache.push_back(x);//没有找到,则新建一个
return IDcache[x]=Setcache.size()-;
} int main(){
int n,kase;
cin>>kase;
while(kase--){
cin>>n;
stack<int > s;
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));//set_union求两个集合的并集
if(op[]=='I') set_intersection(ALL(x1),ALL(x2),INS(x));//set_intersection求两个集合的交集
if(op[]=='A') {x=x2;x.insert(ID(x1));}
s.push(ID(x));
}
cout<<Setcache[s.top()].size()<<endl;
}
cout<<"***"<<"\n";
}
return ;
}

话说这一题目是之前看的了,但是当时始终不理解add操作

,后来看紫书的例子每一个栈里的元素是一个集合,集合又可以套集合,add操作就好理解一些了 就像是这幅插图一样

UVa 12096 The SetStack Computer【STL】的更多相关文章

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

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

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

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

  3. uva 12096 The SetStack Computer

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

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

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

  5. 【最短路】【STL】CSU 1808 地铁 (2016湖南省第十二届大学生计算机程序设计竞赛)

    题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1808 题目大意: N个点M条无向边(N,M<=105),每条边属于某一条地铁Ci ...

  6. 【中途相遇法】【STL】BAPC2014 K Key to Knowledge (Codeforces GYM 100526)

    题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...

  7. 【STL】【模拟】Codeforces 696A Lorenzo Von Matterhorn

    题目链接: http://codeforces.com/problemset/problem/696/A 题目大意: 一个满二叉树,深度无限,节点顺序编号,k的儿子是k+k和k+k+1,一开始树上的边 ...

  8. 蓝桥 ADV-233 算法提高 队列操作 【STL】

      算法提高 队列操作   时间限制:1.0s   内存限制:256.0MB      问题描述 队列操作题.根据输入的操作命令,操作队列(1)入队.(2)出队并输出.(3)计算队中元素个数并输出. ...

  9. 12096 - The SetStack Computer UVA

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

随机推荐

  1. asp.net中XmlDocument解析出现出错,处理特殊字符

    xml结构会解析一些特殊字符,特别是& <  所以我们需要把结构放在CDATA中处理,CDATA里面的内容在XmlDocument 解析时会自动忽略掉,不会解析里面的内容:因此,我这里就 ...

  2. Visual Studio Code asp.net 5环境搭建技能Get

    启动准备阶段 预热 1 VS Code 官方地址:https://www.visualstudio.com/en-us/products/code-vs.aspx 2 安装Node.js :https ...

  3. 不用安装语言包,教你将PS界面语言修改成默认语言(英语)

    地址:http://www.cnblogs.com/Loonger/p/5112020.html 嗯,干脆利落,直接上教程.超简单的方法.(该方法可以随时在你已有语言[非英语]和PS默认语言[英语]之 ...

  4. jquery 取值赋值

    <input type="text" id="range_complete" /> $('#range_complete').val();//取值 ...

  5. 【Leetcode】 - Single Number II

    Problem Discription: Suppose the array A has n items in which all of the numbers apear 3 times excep ...

  6. C# 连接 Oracle 的几种方式[转]

    本文转自:http://www.cnblogs.com/storys/archive/2013/03/06/2945914.html 一:通过System.Data.OracleClient(需要安装 ...

  7. 2064: 分裂 - BZOJ

    Description 背景: 和久必分,分久必和... 题目描述: 中国历史上上分分和和次数非常多..通读中国历史的WJMZBMR表示毫无压力. 同时经常搞OI的他把这个变成了一个数学模型. 假设中 ...

  8. c++ 哪些自定义的数据类型

    http://www.cnblogs.com/ShaneZhang/archive/2013/06/21/3147648.html 这些数据类型是 C99 中定义的,具体定义在:/usr/includ ...

  9. A Step-by-Step Guide to Your First AngularJS App

    What is AngularJS? AngularJS is a JavaScript MVC framework developed by Google that lets you build w ...

  10. pl/sql tutorial

    http://plsql-tutorial.com/plsql-procedures.htm What is PL/SQL? PL/SQL stands for Procedural Language ...