#include<cstdio>
#include<iostream>
#include<cstring>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<string>
#include<sstream>//从串中读取相应格式的变量
#include<algorithm>
#define bug(x) printf("%d***\n",x)
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
using namespace std;
typedef long long llong;
typedef set<int> Set;//将集合映射成数字
map<Set,int> IDcache;//这是集合在vec中的下标
vector<Set> Setcache;//这里面是真正的集合
/*
这题真的太牛了
把不用的集合映射到不同的int值,并且利用下标来保存这个集合的实际情况(当然里面只有个数),
真的太妙了
*/
int ID(Set x){
if(IDcache.count(x)) return IDcache[x];
Setcache.push_back(x);
return IDcache[x]=Setcache.size()-1;
}
int main(){
ios::sync_with_stdio(false);
int T;
cin>>T;
int n;
while(T--){
cin>>n;
stack<int> s;
for(int i=0;i<n;i++){
string op;
cin>>op;
if(op[0]=='P') s.push(ID(Set()));//空集合 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<<"***"<<"\n";
}
return 0;
}
The SetStack Computer UVA - 12096 (五星推荐)
最新推荐文章于 2019-03-21 18:41:22 发布