-
Notifications
You must be signed in to change notification settings - Fork 5
/
UVA-12096.cpp
60 lines (56 loc) · 1.12 KB
/
UVA-12096.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
55
56
57
58
59
60
#include<string>
#include<iostream>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<algorithm>
using namespace std;
map<set<int>, int> mp;
vector<set<int>> v;
stack<int> st;
int findId(set<int> sse) {
if(!mp.count(sse)) {
v.push_back(sse);
mp[sse] = v.size() - 1;
}
return mp[sse];
}
int main() {
int t, n, i;
string s;
cin >> t;
while(t--) {
cin >> n;
while(n--) {
cin >> s;
if(s == "PUSH") {
st.push(findId(set<int>()));
} else if(s == "DUP") {
st.push(st.top());
} else {
int id1 = st.top();
st.pop();
int id2 = st.top();
st.pop();
set<int> sse;
if(s == "UNION") {
set_union(v[id1].begin(), v[id1].end(), v[id2].begin(), v[id2].end(), inserter(sse, sse.begin()));
st.push(findId(sse));
}
if(s == "INTERSECT") {
set_intersection(v[id1].begin(), v[id1].end(), v[id2].begin(), v[id2].end(), inserter(sse, sse.begin()));
st.push(findId(sse));
}
if(s == "ADD") {
sse = v[id2];
sse.insert(id1);
st.push(findId(sse));
}
}
cout << v[st.top()].size() << endl;
}
cout << "***" << endl;
}
return 0;
}