算典05_例题_05_UVA-12096
2017-04-02 16:48
363 查看
The SetStack Computer
题意
有一个专门为了集合运算而设计的“集合栈”计算机。该机器有一个初始为空的栈,并且支持以下操作:PUSH:空集“{}”入栈
DUP:把当前栈顶元素复制一份后再入栈
UNION:出栈两个集合,然后把两者的并集入栈
INTERSECT:出栈两个集合,然后把二者的交集入栈
ADD:出栈两个集合,然后把先出栈的集合加入到后出栈的集合中,把结果入栈
每次操作后,输出栈顶集合的大小(即元素个数)。
例如栈顶元素是A={ {}, {{}} }, 下一个元素是B={ {}, {{{}}} },则:
UNION操作将得到{ {}, {{}}, {{{}}} },输出3.
INTERSECT操作将得到{ {} },输出1
ADD操作将得到{ {}, {{{}}}, { {}, {{}} } },输出3.
题解
这道题对于理解map和set甚是有用说白了,这道题的核心是如何表示“集合的集合”
如果我们直接用set < set<> > 会很麻烦,这里不妨只保存集合的下标,即用一个set < int > 来表示某个集合,它里面的元素为其他集合的下标,那么它就是一个集合的集合了
但是还有一点很重要,如果两个集合中的元素是一样的,那么我们认为它们是同一个集合,即它们有同一个下标,这样才能保证集合交和并的正确性
所以我们需要一个映射map < Set, int > m 来表示一个集合到下标的映射,这样只要判断m.count(某个Set)即可知道这个集合是否出现过了,只有当它从未出现过的时候才给它新的编号
这是我自己写的代码,也AC了,但是不如书上用的巧妙
#include <iostream> #include <cstdio> #include <queue> #include <cstring> #include <algorithm> #include <cmath> #include <vector> #include <string> #include <sstream> #include <set> #include <map> using namespace std; const int maxn = 2e6 + 5; const int INF = (1<<31)-1; #define met(a, b) memset(a, b, sizeof(a)); #define IN freopen("in.txt", "r", stdin); typedef long long LL; typedef set<int> Set; int t, n; char op[10]; int stake[maxn], top, sid; Set s[maxn]; map<Set, int> m; void solve() { if(op[0] == 'P') { if(!m.count(s[sid]))m[s[sid]] = sid, stake[++top] = sid++; else stake[++top] = m[s[sid]]; } else if(op[0] == 'D') { int a = stake[top]; stake[++top] = a; } else if(op[0] == 'U') { int a = stake[top--]; int b = stake[top--]; Set x = s[a]; for(set<int>::iterator it = s[b].begin(); it!=s[b].end();++it) { if(!x.count(*it)) x.insert(*it); } if(!m.count(x))m[x] = sid, s[sid] = x, stake[++top] = sid++; else stake[++top] = m[x]; } else if(op[0] == 'I') { int a = stake[top--]; int b = stake[top--]; Set x; for(set<int>::iterator it = s[b].begin(); it!=s[b].end();++it) { if(s[a].count(*it)) x.insert(*it); } if(!m.count(x))m[x] = sid, s[sid] = x, stake[++top] = sid++; else stake[++top] = m[x]; } else if(op[0] == 'A') { int a = stake[top--]; int b = stake[top--]; Set x = s[b]; x.insert(a); if(!m.count(x))m[x] = sid, s[sid] = x, stake[++top] = sid++; else stake[++top] = m[x]; } } int main(){ #ifdef _LOCAL IN; #endif // _LOCAL cin >> t; while(t--) { top = 0; sid = 0; m.clear(); scanf("%d", &n); for(int i = 0; i < n; ++i) s[i].clear(); for(int i = 0; i < n; ++i) { cin >> op; solve(); cout << s[stake[top]].size() <<endl; } cout <<"***" <<endl; } return 0; }
这是书上的代码
#include <iostream> #include <cstdio> #include <queue> #include <cstring> #include <algorithm> #include <cmath> #include <vector> #include <string> #include <sstream> #include <set> #include <map> #include <stack> using namespace std; const int maxn = 2e6 + 5; const int INF = (1<<31)-1; #define met(a, b) memset(a, b, sizeof(a)); #define IN freopen("in.txt", "r", stdin); typedef long long LL; typedef set<int> Set; //STL内置的集合操作 #define ALL(x) x.begin(), x.end() #define INS(x) inserter(x,x.begin()) map<Set, int> m; //把集合映射成ID vector<Set> S; //记录下来的set集 //根据集合找下标,没有的话分配新的 int ID(Set x) { if(m.count(x)) return m[x]; S.push_back(x); return m[x] = S.size() - 1; } stack<int> s; int n; char op[10]; int main(){ #ifdef _LOCAL IN; #endif // _LOCAL int t; cin >> t; while(t--) { cin >> n; for(int i = 0; i < n; ++i) { cin >> op; if(op[0] == 'P') s.push(ID(Set())); else if(op[0] == 'D') s.push(s.top()); else { Set x1 = S[s.top()]; s.pop(); Set x2 = S[s.top()]; s.pop(); Set x; if(op[0] == 'U') set_union(ALL(x1), ALL(x2), INS(x)); if(op[0] == 'I') set_intersection(ALL(x1), ALL(x2), INS(x)); if(op[0] == 'A') x = x2, x.insert(ID(x1)); s.push(ID(x)); } cout << S[s.top()].size() <<endl; } cout << "***" << endl; } return 0; }
其实也没差哪去,只是不如书上的美观而已^0^
相关文章推荐
- 算典05_例题_07_UVA-136
- 算典04_例题_05_UVA-512
- 算典05_例题_08_UVA-400
- 算典05_例题_12_UVA-221
- 算法竞赛入门经典第五章例题5-5 The SetStack Computer UVA - 12096
- 算法竞赛入门经典(第2版)例题5-5 集合栈计算机 (The SetStack Computer UVa 12096)
- uva 12096 例题5-5 map vector set stack 混用
- 例题 5-5 集合栈计算机 Uva 12096
- 【例题5-5 UVA 12096 】The SetStack Computer
- 算典05_例题_02_UVA-101
- 算典05_例题_03_UVA-10815
- 算典05_例题_09_UVA-1592
- 算典05_例题_04_UVA-156
- 算典05_例题_06_UVA-540
- 算典05_例题_10_UVA-207
- 算典03_例题_05_Uva-1583
- 例题5-5 UVA 12096 The SetStack Computer集合栈计算机
- 算典05_例题_11_UVA-814
- UVa 12096 (STL) The SetStack Computer
- uva572 - Oil Deposits 入门经典II 第六章数据结构基础 例题6-12