题目连接:
传送门
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<map>
#include<vector>
#include<set>
#include<stack>
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
using namespace std;
typedef set<int> Set;
map<Set,int> mmp;
vector<Set> vec;
stack<int>sta;
int isset(Set x)
{
if(mmp.count(x)) return mmp[x];
vec.push_back(x);
return mmp[x]=vec.size()-1;
}
int main()
{
int T;
cin>>T;
while(T--){
mmp.clear();
while(!sta.empty())sta.pop();
vec.clear();
int n;
cin>>n;
for(int i=0;i<n;i++)
{
string str;
cin>>str;
if(str=="PUSH")sta.push(isset(Set()));
else if(str=="DUP") sta.push(sta.top());
else
{
Set x1=vec[sta.top()];sta.pop();
Set x2=vec[sta.top()];sta.pop();
Set x;
if(str=="UNION") set_union(ALL(x1),ALL(x2),INS(x));
if(str=="INTERSECT") set_intersection(ALL(x1), ALL(x2), INS(x));
if(str=="ADD") {
x=x2;x.insert(isset(x1));
}
sta.push(isset(x));
}
cout<<vec[sta.top()].size()<<endl;
}
cout<<"***"<<endl;
}
return 0;
}