难度:3
这道题有点抽象啊,还不是很理解,只是知道它是这么分配id的,还要好好看看。
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pa;
vector<set<int> > v;
map<set<int>, int> mp;
int getid(set<int> st) {
if (mp.find(st) != mp.end()) return mp[st];
v.pb(st);
return mp[st] = v.size() - 1;
}
int main() {
int t;
cin >> t;
while (t--) {
v.clear();
mp.clear();
int n;
cin >> n;
stack<int> s;
for (int i = 0; i < n; i++) {
string op;
cin >> op;
if (op[0] == 'P') s.push(getid(set<int>()));
else if (op[0] == 'D') s.push(s.top());
else {
set<int> s1, s2;
s1 = v[s.top()]; s.pop();
s2 = v[s.top()]; s.pop();
set<int> ss;
if (op[0] == 'U') set_union(all(s1), all(s2), inserter(ss, ss.begin()));
else if (op[0] == 'I') set_intersection(all(s1), all(s2), inserter(ss, ss.begin()));
else {
ss = s2;
ss.insert(getid(s1));
}
s.push(getid(ss));
}
cout << v[s.top()].size() << endl;
}
cout << "***\n";
}
return 0;
}