UVa #12096 The SetStack Computer (例题5-5)
来源:互联网 发布:excel单元格数据关联 编辑:程序博客网 时间:2024/06/10 13:49
这道题的思想在后面用到了几次,因此还是贴出来:
这道题是一个编码问题,即将某种数据结构通过一定的方法转化为独特的整数。这样就可以用整数来进行操作,大大简化了程序。
对于这道题中的集合来说,一个集合包含了0或多个集合,并且可能有多层嵌套,无法用STL的set来模拟。但是我们可以给每一种集合分配一个ID。比如我们给{}分配1,{{}}就可以表示成{1},我们给他分配2。{{},{}}就可以表示为{1, 1},我们给他分配3。{ {} , {{}} , {{},{}} }就可以表示为{1, 2, 3},我们可以给这个集合分配4。这样,我们就可以把集合的集合变成整数的集合,即便有再多嵌套,都可以用一个简单的整数来表示。
于是在读入操作的时候,我们判断新生成的集合是否出现过。如果出现过,直接用它对应的ID替换他。如果没出现过,则给他分配一个新的ID,并把它存在数组中ID的位置上
判断最顶部集合的大小,只需要根据ID去数组中找到这个集合,并查看这个集合中多少个元素。
Run Time: 0.415s
#define UVa "LT5-5.12096.cpp"//The SetStack Computerchar fileIn[30] = UVa, fileOut[30] = UVa;#include<cstring>#include<cstdio>#include<algorithm>#include<vector>#include<set>#include<stack>#include<map>#include<ctime>#include<iostream>using namespace std;#define ALL(x) x.begin(), x.end()#define INS(x) inserter(x, x.begin())//Global Variables. Reset upon Each Case!int T, N, cnt;typedef set<int> Set;map<Set, int> m;vector<Set> v;char op[20];/////int getID(Set s) { if(m.count(s)) return m[s]; v.push_back(s); m[s] = cnt++; return m[s];}int main() { scanf("%d", &T); while(T--) { scanf("%d", &N); stack<int> stk; cnt = 0; v.clear(); m.clear(); for(int i = 0; i < N; i ++) { scanf("%s", op); int empty = getID(Set()); if(op[0] == 'P') { stk.push(empty); } else if(op[0] == 'D') { stk.push(stk.top()); } else { int a = stk.top(); stk.pop(); int b = stk.top(); stk.pop(); Set sa = v[a], sb = v[b]; int u = 0; Set su = Set(); if(op[0] == 'U') { set_union(ALL(sa), ALL(sb), INS(su)); u = getID(su); } else if(op[0] == 'I') { set_intersection(ALL(sa), ALL(sb), INS(su)); u = getID(su); } else if(op[0] == 'A') { sb.insert(getID(sa)); u = getID(sb); } stk.push(u); } cout<<v[stk.top()].size()<<endl; } cout<<"***"<<endl; } return 0;}
0 0
- UVa #12096 The SetStack Computer (例题5-5)
- 例题5-5 UVA 12096 The SetStack Computer集合栈计算机
- 算法竞赛入门经典(第2版)例题5-5 集合栈计算机 (The SetStack Computer UVa 12096)
- uva 12096 The SetStack Computer
- UVA - 12096 The SetStack Computer
- UVa 12096 - The SetStack Computer
- UVa 12096 The SetStack Computer
- Uva - 12096 - The SetStack Computer
- UVa-12096 The SetStack Computer
- UVA 12096 The SetStack Computer
- UVA 12096 - The SetStack Computer
- UVa 12096 - The SetStack Computer
- UVA-12096 The SetStack Computer
- UVA 12096 The SetStack Computer
- Uva 12096 The SetStack Computer
- UVa 12096 The SetStack Computer
- UVA-12096 The SetStack Computer
- Uva 12096 The SetStack Computer
- js解析xml字符串
- 关于Serializable的serialVersionUID
- 【Tools】【API】在线API
- python 正则表达式 re
- 深度优先遍历与广度优先遍历 递归与非递归思路
- UVa #12096 The SetStack Computer (例题5-5)
- SourceInsight 3.x 注册码
- serialSerializable 与 transient
- (8.1.4)软件测试之基础理论
- 命令行查看ubuntu版本号
- 复习SpringMVC 容器实例化 注解注入等
- java 中json对象解析
- JAXB学习笔记
- mxgraph入门 之 mxcell的操作方法