首页 > 其他 > 详细

集合栈计算机 (The SetStack Computer,ACM/ICPC NWERC 2006,UVa12096

时间:2018-08-16 23:35:05      阅读:396      评论:0      收藏:0      [点我收藏+]

题目描述:

技术分享图片

 1 #include<iostream>
 2 #include<string>
 3 #include<set>
 4 #include<map>
 5 #include<stack>
 6 #include<vector>
 7 #include<algorithm>
 8 using namespace std;
 9 
10 #define ALL(x) x.begin(),x.end()
11 #define INS(x) inserter(x,x.begin())
12 
13 typedef set<int> Set;
14 map<Set,int> IDcache; // 把集合映射成ID
15 vector<Set> Setcache; // 根据ID取集合
16 
17 int ID(Set x){
18     if(IDcache.count(x)) return IDcache[x] ;
19     Setcache.push_back(x) ;
20     return IDcache[x] = Setcache.size() - 1;
21 }
22 
23 int main(){
24     int t ;
25     cin >> t ;
26     while(t--){
27         stack<int> s;
28         int n;
29         cin>> n;
30         for(int i=0;i<n;i++){
31             string op;
32             cin >> op;
33             if(op[0] == P) s.push(ID(Set())) ;
34             else if (op[0] == D) s.push(s.top());
35             else{
36                 Set x1 = Setcache[s.top()] ; s.pop();
37                 Set x2 = Setcache[s.top()] ;s.pop() ;
38                 Set x ;
39                 if (op[0] == U) set_union (ALL(x1), ALL(x2), INS(x));
40                     if (op[0] == I) set_intersection (ALL(x1), ALL(x2), INS(x));
41                 if (op[0] == A) { x = x2; x.insert(ID(x1)); }
42                 s.push(ID(x));
43             }
44             cout << Setcache[s.top()].size() << endl;
45         }
46         cout << "***" << endl;
47     }
48     return 0;
49 }

 

集合栈计算机 (The SetStack Computer,ACM/ICPC NWERC 2006,UVa12096

原文:https://www.cnblogs.com/secoding/p/9490684.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!