本题最有价值的部分是这个ID函数
map<Set, int>IDcache;
vector<Set>Setache;
int ID(Set s)
{
if (IDcache.count(s))return IDcache[s];
Setache.push_back(s);
return IDcache[s] = Setache.size() - 1;
}
通过这个函数可以讲任意变量映射为一个int值,并通过这个int值可以在vector中查找到映射值所对的变量
ac代码
#include<iostream>
#include<cstring>
#include<queue>
#include<set>
#include<algorithm>
#include<sstream>
#include<map>
#include<stack>
#include<vector>
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
const int N = 1e5 + 10;
using namespace std;
#define FAST ios::sync_with_stdio(false)
#define int long long
#define endl '\n'
typedef set<int>Set;
map<Set, int>IDcache;
vector<Set>Setache;
stack<int>s;
int ID(Set s)
{
if (IDcache.count(s))return IDcache[s];
Setache.push_back(s);
return IDcache[s] = Setache.size() - 1;
}
signed main()
{
FAST;
int _;
cin >> _;
while (_--)
{
Setache.clear();
IDcache.clear();
while (!s.empty())s.pop();
int n;
cin >> n;
while (n--)
{
string op;
cin >> op;
if (op[0] == 'P')s.push(ID(Set()));
else if (op[0] == 'D')s.push(s.top());
else {
Set x1 = Setache[s.top()]; s.pop();
Set x2 = Setache[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 << Setache[s.top()].size() << endl;
}
cout << "***" << endl;
}
}