-
Notifications
You must be signed in to change notification settings - Fork 1
/
UVA12096.cpp
54 lines (49 loc) · 1.38 KB
/
UVA12096.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <set>
#include <map>
#include <vector>
#include <stack>
#include <algorithm> // contains std::set_union std::set_intersection
#define ALL(x) x.begin(), x.end()
#define INS(x) inserter(x, x.begin())
using namespace std;
typedef set<int> Set;
map<Set, int> IDcache; // 集合映射为 ID
vector<Set> Setcache; // 根据 ID 取集合
// 查找 ID ,如找不到,则分配新 ID
int ID (Set x)
{
if (IDcache.count(x)) return IDcache[x];
Setcache.push_back(x);
return IDcache[x] = Setcache.size() - 1;
}
int main()
{
int kase;
cin >> kase;
for (int i = 0; i < kase; i++)
{
stack<int> s;
int n;
cin >> n;
for (int j = 0; j < n; j++)
{
string op;
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 tmp;
if (op[0] == 'U') set_union (ALL(x1), ALL(x2), INS(tmp));
if (op[0] == 'I') set_intersection (ALL(x1), ALL(x2), INS(tmp));
if (op[0] == 'A') { tmp = x2; tmp.insert(ID(x1)); }
s.push(ID(tmp));
}
cout << Setcache[s.top()].size() << endl;
}
cout << "***\n";
}
}