UVa 12096 The SetStack Computer【STL】
2015-03-22 21:21
429 查看
题意:给出一个空的栈,支持集合的操作,求每次操作后,栈顶集合的元素个数
从紫书给的例子
A={{},{{}}}
B={{},{{{}}}}
A是栈顶元素,A是一个集合,同时作为一个集合的A,它自身里面也可以集合套集合
View Code
话说这一题目是之前看的了,但是当时始终不理解add操作
,后来看紫书的例子每一个栈里的元素是一个集合,集合又可以套集合,add操作就好理解一些了 就像是这幅插图一样
从紫书给的例子
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()-1; } int main(){ int n,kase; cin>>kase; while(kase--){ cin>>n; stack<int > s; for(int i=0;i<n;i++){ string op; cin>>op; if(op[0]=='P') s.push(ID(Set())); else if(op[0]=='D') s.push(s.top()); else{ Set x1=Setcache[s.top()];s.pop(); Set x2=Setcache[s.top()];s.pop(); Set x; if(op[0]=='U') set_union(ALL(x1),ALL(x2),INS(x));//set_union求两个集合的并集 if(op[0]=='I') set_intersection(ALL(x1),ALL(x2),INS(x));//set_intersection求两个集合的交集 if(op[0]=='A') {x=x2;x.insert(ID(x1));} s.push(ID(x)); } cout<<Setcache[s.top()].size()<<endl; } cout<<"***"<<"\n"; } return 0; }
View Code
话说这一题目是之前看的了,但是当时始终不理解add操作
,后来看紫书的例子每一个栈里的元素是一个集合,集合又可以套集合,add操作就好理解一些了 就像是这幅插图一样
![](http://images.cnitblog.com/blog2015/666744/201503/222120186256517.jpg)
相关文章推荐
- UVa 12096 (STL) The SetStack Computer
- UVa 12096 The SetStack Computer 【STL】【stack】
- UVA12096 the SetStack Computer(STL中,map,set,vector,stack联合应用)
- UVA - 12096 The SetStack Computer STL的大集合
- uva 12096 The SetStack Computer(STL)
- uva 12096 - The SetStack Computer(STL)
- uva 12096 - The SetStack Computer(STL)
- uva 12096 - The SetStack Computer(STL 的运用)
- uva 12096 - The SetStack Computer(STL 的运用)
- UVa - 12096 The SetStack Computer(STL容器综合,强推!)
- UVA.12096 The SetStack Computer ( 好题 栈 STL混合应用)
- UVA - 12096 The SetStack Computer(编码,STL)
- UVa-12096 The SetStack Computer
- UVAOJ 12096 The SetStack Computer(STL的运用)
- Uva 12096 The SetStack Computer
- 算法竞赛入门经典(第2版)例题5-5 集合栈计算机 (The SetStack Computer UVa 12096)
- uva 12096 The SetStack Computer
- 【Uva 12096】The SetStack Computer
- Problem 032——UVa 12096 - The SetStack Computer
- UVA 12096(p115)----The SetStack Computer