您的位置:首页 > 理论基础

UVa12096(集合中的集合+stl)集合栈计算机

2017-07-31 10:57 381 查看


//一个集合代表一个数字,神奇的操作
#include<set>
#include<iostream>
#include<map>
#include<string>
#include<vector>
#include<stack>
#include<algorithm>
using namespace std;
string str;
stack<int> s;
typedef set<int> Set;
map<Set,int> mp;
vector<Set> setcache;
int ID(Set x)
{
//查找集合x的编号,如果没找到,就为其新建一个编号
if(mp.count(x)) return mp[x];
setcache.push_back(x);
return mp[x]=setcache.size()-1;
}
int main()
{
int T,n;
cin>>T;
while(T--)
{
scanf("%d",&n);
while(n--)
{
cin>>str;
if(str[0]=='P') s.push(ID(Set()));//空集入栈
else if(str[0]=='D') s.push(s.top());
else
{
Set a,b,c;
a=setcache[s.top()];
s.pop();
b=setcache[s.top()];
s.pop();
if(str[0]=='U')
set_union(a.begin(),a.end(),b.begin(),b.end(),inserter(c,c.begin()));
if(str[0]=='I')
set_intersection(a.begin(),a.end(),b.begin(),b.end(),inserter(c,c.begin()));
if(str[0]=='A')
{
b.insert(ID(a));
c=b;
}
s.push(ID(c));
}
cout<<setcache[s.top()].size()<<endl;
}
puts("***");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: