思路:
刘汝佳老师的,啊,说了很重要,但是本蒟弱还是没理解透理解了的部分写在注释上了,以后补充,
忘大佬指点
#include<iostream>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<string>
#include <algorithm>
using namespace std;
typedef set<int> Set;//含若干个元素的集合
map<Set,int> IDcache;//IDcache[s]代表某个集合的ID,
vector<Set> Setcache;//对于某个集合Setcache[ID]这里面还是ID的集合
//带参数的宏定义,这个意思是 ALL(x) = x.begin(),x.end()
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
int ID(Set x)//这里返回的是集合x在Setcache的下标
{
//如果这个集合已经有ID了则直接return
if(IDcache[x]) return IDcache[x];
//否则,先添加新集合
Setcache.push_back(x);
return IDcache[x] = Setcache.size() - 1;
}
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
//栈里面存的是依次递增的数字
//代表某集合在Setcache里的下标
stack<int> s;
for(int i=0;i<n;i++)
{
string op;//操作operation
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;
//将x1和x2取并集,并且放在集合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)) ;}//将x1的D插入到x中
s.push(ID(x));
}
//for(int j=0;j<Setcache.size();j++)
//{
// cout<<Setcache[j].size()<<" ";
// }
// cout<<endl;
// cout<<"栈:";
//cout<<s.size()<<endl;
cout<<Setcache[s.top()].size()<<endl;
}
printf("***\n");
}
return 0;
}