标签:
Description
Input
Output
Sample Input
2 9 PUSH DUP ADD PUSH ADD DUP ADD DUP UNION 5 PUSH PUSH ADD PUSH INTERSECT
Sample Output
0 0 1 0 1 1 2 2 2 *** 0 0 1 0 0 ***
1 #include <iostream> 2 #include <set> 3 #include <map> 4 #include <vector> 5 #include <stack> 6 #include <algorithm> 7 using namespace std; 8 9 map<set<int>, int> id; 10 map<set<int>, int>::iterator it; 11 vector<set<int> > v; 12 13 int getID(set<int> x) 14 { 15 it = id.find(x); 16 if(it != id.end()) 17 return it->second; 18 v.push_back(x); 19 return id[x] = v.size()-1; 20 } 21 22 int main() 23 { 24 int T, n; 25 cin>>T; 26 while(T--){ 27 cin>>n; 28 id.clear(); 29 v.clear(); 30 stack<int> s; 31 string op; 32 while(n--){ 33 cin>>op; 34 if(op[0] == ‘P‘) 35 s.push(getID(set<int>())); 36 else if(op[0] == ‘D‘) 37 s.push(s.top()); 38 else{ 39 set<int> t1 = v[s.top()]; s.pop(); 40 set<int> t2 = v[s.top()]; s.pop(); 41 set<int> t; 42 if(op[0] == ‘U‘){ 43 set_union(t1.begin(), t1.end(), t2.begin(), t2.end(), inserter(t, t.begin())); 44 s.push(getID(t)); 45 } 46 else if(op[0] == ‘I‘){ 47 set_intersection(t1.begin(), t1.end(), t2.begin(), t2.end(), inserter(t, t.begin())); 48 s.push(getID(t)); 49 } 50 else if(op[0] == ‘A‘){ 51 t2.insert(getID(t1)); 52 s.push(getID(t2)); 53 } 54 } 55 cout<<v[s.top()].size()<<endl; 56 } 57 cout<<"***"<<endl; 58 } 59 return 0; 60 }
UVa 12096 The SetStack Computer
标签:
原文地址:http://www.cnblogs.com/inmoonlight/p/5701630.html