题意:
定义了5种操作,模拟相应操作即可
思路:
本题集合中存放的还是集合,故需要特殊方法,将每个集合映射到一个int编号,此题比较复杂,要仔细思考,详见代码
注意此题这种特殊处理方法,思路很重要
代码:
#include <iostream>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <algorithm>
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
using namespace std;
typedef set<int>Set; //Set中存放此集合包含的其它集合的id
map<Set,int> IDcache; //将一个set映射到唯一的一个id
vector<Set> Setcache; //通过id找到相应集合
int ID(Set x) //寻找id,若没有即是新集合,为其生成一个新id
{
if(IDcache.count(x))
return IDcache[x];
Setcache.push_back(x);
return IDcache[x]=Setcache.size()-1;
}
int main()
{
int t;
cin>>t;
while(t--)
{
IDcache.clear();
Setcache.clear();
stack<int>s;
int n;
cin>>n;
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));
if(op[0]=='I') set_intersection(ALL(x1),ALL(x2),INS(x));
if(op[0]=='A')
{
x=x2;
x.insert(ID(x1));
}
s.push(ID(x));
}
cout<<Setcache[s.top()].size()<<endl;
}
cout<<"***"<<endl;
}
return 0;
}