题目描述:

1 #include<iostream>
2 #include<string>
3 #include<set>
4 #include<map>
5 #include<stack>
6 #include<vector>
7 #include<algorithm>
8 using namespace std;
9
10 #define ALL(x) x.begin(),x.end()
11 #define INS(x) inserter(x,x.begin())
12
13 typedef set<int> Set;
14 map<Set,int> IDcache; // 把集合映射成ID
15 vector<Set> Setcache; // 根据ID取集合
16
17 int ID(Set x){
18 if(IDcache.count(x)) return IDcache[x] ;
19 Setcache.push_back(x) ;
20 return IDcache[x] = Setcache.size() - 1;
21 }
22
23 int main(){
24 int t ;
25 cin >> t ;
26 while(t--){
27 stack<int> s;
28 int n;
29 cin>> n;
30 for(int i=0;i<n;i++){
31 string op;
32 cin >> op;
33 if(op[0] == 'P') s.push(ID(Set())) ;
34 else if (op[0] == 'D') s.push(s.top());
35 else{
36 Set x1 = Setcache[s.top()] ; s.pop();
37 Set x2 = Setcache[s.top()] ;s.pop() ;
38 Set x ;
39 if (op[0] == 'U') set_union (ALL(x1), ALL(x2), INS(x));
40 if (op[0] == 'I') set_intersection (ALL(x1), ALL(x2), INS(x));
41 if (op[0] == 'A') { x = x2; x.insert(ID(x1)); }
42 s.push(ID(x));
43 }
44 cout << Setcache[s.top()].size() << endl;
45 }
46 cout << "***" << endl;
47 }
48 return 0;
49 }