您的位置:首页 > 其它

UVa 12096 The SetStack Computer【STL】

2015-03-22 21:21 429 查看
题意:给出一个空的栈,支持集合的操作,求每次操作后,栈顶集合的元素个数

从紫书给的例子

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操作就好理解一些了 就像是这幅插图一样

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: