UVa 12096 The SetStack Computer【STL】
题意:给出一个空的栈,支持集合的操作,求每次操作后,栈顶集合的元素个数
从紫书给的例子
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】的更多相关文章
- uva 12096 The SetStack Computer(STL set的各种库函数 交集 并集 插入迭代器)
题意: 有5种操作: PUSH:加入“{}”空集合入栈. DUP:栈顶元素再入栈. UNION:出栈两个集合,取并集入栈. INTERSECT:出栈两个集合,取交集入栈. ADD:出栈两个集合,将先出 ...
- 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(集合栈)
例题5-5 集合栈计算机(The Set Stack Computer,ACM/ICPC NWERC 2006,UVa12096) 有一个专门为了集合运算而设计的"集合栈"计算机. ...
- 【最短路】【STL】CSU 1808 地铁 (2016湖南省第十二届大学生计算机程序设计竞赛)
题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1808 题目大意: N个点M条无向边(N,M<=105),每条边属于某一条地铁Ci ...
- 【中途相遇法】【STL】BAPC2014 K Key to Knowledge (Codeforces GYM 100526)
题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...
- 【STL】【模拟】Codeforces 696A Lorenzo Von Matterhorn
题目链接: http://codeforces.com/problemset/problem/696/A 题目大意: 一个满二叉树,深度无限,节点顺序编号,k的儿子是k+k和k+k+1,一开始树上的边 ...
- 蓝桥 ADV-233 算法提高 队列操作 【STL】
算法提高 队列操作 时间限制:1.0s 内存限制:256.0MB 问题描述 队列操作题.根据输入的操作命令,操作队列(1)入队.(2)出队并输出.(3)计算队中元素个数并输出. ...
- 12096 - The SetStack Computer UVA
Background from Wikipedia: \Set theory is a branch of mathematics created principally by the German ...
随机推荐
- BAT CMD 批处理文件脚本 -1
http://www.cnblogs.com/linglizeng/archive/2010/01/29/Bat-CMD-ChineseVerion.html 1. 综述 ...
- Eclipse 环境安装和配置优化
1.下载相应的eclipse版本. 官方下载地址:http://www.eclipse.org/downloads/ juno版本的64位下载地址:http://www.eclipse.o ...
- 【锋利的JQuery-学习笔记】切换网页皮肤-且保存于Cookie
切换网页皮肤: html片段: <head> <link rel="stylesheet" href="styles/skin/skin_0.css&q ...
- uva 10912
dp 记忆化搜索 #include <cstdio> #include <cstdlib> #include <cmath> #include <map> ...
- Unity3dBug - OnEnable
最近 项目 因为 使用 active 代替 instantiate机制,很多时候 OnEnable 代理 OnStart. 然后发现一个 奇怪的 问题 void Awake() { Debug.Log ...
- JsRender系列demo(4)-if else
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- 用 Xamarin for VS 创建 aar 文件的绑定
预备工作:相关aar文件,Xamarin for VS一份.我这里以Android中挺火的 MaterialDesignLibrary 为例. 1.首先,创建一个Xamarin Binding Lib ...
- hdu 3123 GCC
这题分2种情况: 1) n>=m时,k!%m=0(k>=m),所以只需令n=m-1即可: 2) n<m时,正常情况处理即可. ;}
- hdu 1275 两车追及或相遇问题
思路:这里有2种情况: 一种是相遇:满足关系是 (va+vb)*t=L*(2*n-1) 一种是追及: 满足关系是 |va-vb|*t=L*(2*n-1) 这样求出2种情况的时间,在排序就可以了…… 链 ...
- Android 程序运行之后,禁止休眠
在程序中添加 // 禁止休眠功能 getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager. ...