UVa12096

題目:
https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3248

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
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <bits/stdc++.h>
using namespace std;

stack<int> st; // the main stack
map<set<int>, int> mp; // key: set, value: id
vector<set<int>> v; // id is the index
int id=0;

int getID(set<int> &s){
auto it = mp.find(s);
if(it != mp.end()) return it->second;
// if(mp.count(s)) return mp[s];

v.push_back(s);
return mp[s] = id++;

}

int main(){
ios::sync_with_stdio(0);
cin.tie(0);
int kase;
cin >> kase;

int n;
string cmd;
set<int> s;
while(kase--){
cin >> n;

id = 0;
while(!st.empty()) st.pop();
mp.clear();
v.clear();

while(n--){
s.clear();
cin >> cmd;
// question guarantees won't pop empty stack
char ch = cmd[0];
if(ch == 'P') st.push(getID(s)); // PUSH
else if(ch == 'D') st.push(st.top()); // DUPLICATE
else{
int id1 = st.top(); st.pop();
int id2 = st.top(); st.pop();

if(ch == 'U'){ // UNION
set_union(v[id1].begin(), v[id1].end(), v[id2].begin(), v[id2].end(), inserter(s, s.begin()));
/*
for(auto num : v[id1]) s.insert(num);
for(auto num : v[id2]) s.insert(num);
*/
}
else if(ch == 'I'){ // INTERSECT
set_intersection(v[id1].begin(), v[id1].end(), v[id2].begin(), v[id2].end(), inserter(s, s.begin()));
/*
for(auto num : v[id1]){
if(v[id2].count(num)) s.insert(num);
}
*/
}
else{ // ADD
s = v[id2];
s.insert(id1);
}
st.push(getID(s));
}
cout << v[st.top()].size() << '\n'; // each round, output the top stack's size
}
cout << "***\n";
}
return 0;
}