UVA 12096 The SetStack Computer
阿新 • • 發佈:2019-02-07
一次 cto span 兩個 compute bsp vector turn 查找
1 #include "iostream" 2 #include "vector" 3 #include "set" 4 #include "map" 5 #include "stack" 6 #include "string" 7 #include "algorithm" 8 #include "iterator" 9 using namespace std; 10 #define ALL(x) x.begin(),x.end()//宏 11 #define INS(x) inserter(x,x.begin())//宏 12 typedef set <int> Set;13 map<Set, int> IDcache;//集合映射成ID 14 vector<Set> Setcache;//根據ID取集合 15 int ID(Set x)//查找集合x的ID,找不到分配新的 16 { 17 if (IDcache.count(x)) 18 return IDcache[x]; 19 Setcache.push_back(x);//添加新集合 20 return IDcache[x] = Setcache.size() - 1; 21 } 22 int main() 23 { 24 stack<int> s;25 int m; 26 cin >> m; 27 while (m--) 28 { 29 int n; 30 cin >> n; 31 for (int i = 0; i < n; i++) 32 { 33 string op; 34 cin >> op; 35 if (op[0] == ‘P‘)//PUSH 36 s.push(ID(Set()));//空集入棧 37else 38 if (op[0] == ‘D‘)//DUP,棧頂復制一次再入棧 39 s.push(s.top()); 40 else 41 { 42 Set x1 = Setcache[s.top()]; 43 s.pop(); 44 Set x2 = Setcache[s.top()]; 45 s.pop();//出棧兩個集合 46 Set x; 47 if (op[0] == ‘U‘)//出棧兩個集合,並集入棧 48 set_union(ALL(x1), ALL(x2), INS(x)); 49 //獲得兩個集合的並集。兩個輸入序列須保證已排好序 50 if (op[0] == ‘I‘)//出棧兩個集合,交集入棧 51 set_intersection(ALL(x1), ALL(x2), INS(x)); 52 if (op[0] == ‘A‘)//出棧兩個集合,先出棧的集合加到 53 { //後出棧的集合中,再入棧 54 x = x2; 55 x.insert(ID(x1)); 56 } 57 s.push(ID(x)); 58 } 59 cout << Setcache[s.top()].size() << endl; 60 } 61 //if (m != 0) 62 cout << "***" << endl; 63 } 64 return 0; 65 }
UVA 12096 The SetStack Computer