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

紫书章五例题五 集合栈计算机 UVA 12096(stack)

2017-04-03 12:19 429 查看
这个题的特点进行集合运算,stack中放的是集合。

然后我们用vector将集合存起来,将对应的下标来代表集合。用map来实现对应关系。其中使用set来存一个集合,因为集合中不能有重复的元素。

stack a

a.top();

a.push();

a.pop();

a.empty();

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <set>
#include <stack>
#include <map>
#include <vector>
using namespace std;
//typedef set<int> Set;
char s[30];
set<int> t;
map<set<int>,int> M;
stack<int> sta;
vector< set<int> > v;
int num(set<int> k)
{
if(M.count(k)) return M[k];
v.push_back(k);
M[k]=v.size()-1;
return M[k];
}
int main()
{
//  freopen("E:\\input.txt","r",stdin);
int n;
scanf("%d",&n);
while(n--){
v.clear();
while(!sta.empty()) sta.pop();
M.clear();
int m;
scanf("%d",&m);
for(int i=0;i<m;i++){
scanf("%s",s);
if(s[0]=='P')
sta.push(num(t));
else if(s[0]=='D')
sta.push(sta.top());
else
{
int a=sta.top();sta.pop();
int b=sta.top();sta.pop();
set<int> t1=v[a],t2=v[b],t3;
if(s[0]=='A'){
t3=t2;
t3.insert(num(t1));
sta.push(num(t3));
}
else if(s[0]=='U'){
set<int> ::iterator it;
t3=t2;
for(it=t1.begin();it!=t1.end();it++)
t3.insert(*it);
sta.push(num(t3));
}
else if(s[0]=='I'){

4000
set<int> ::iterator it;
for(it=t1.begin();it!=t1.end();it++){
if(t2.count(*it)) t3.insert(*it);
}
sta.push(num(t3));
}
}
int g=sta.top();
cout<<v[g].size()<<endl;
}
cout<<"***"<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: