The SetStack Computer集合栈计算器(stack的运用)
来源:互联网 发布:拆分系统源码下载 编辑:程序博客网 时间:2024/06/05 19:27
The SetStack Computer
有一个专门为了集合运算而设计的“集合栈”计算机。该机器有一个初始为空的栈,并且支持以下操作:
PUSH:空集“{}”入栈
DUP:把当前栈顶元素复制一份后再入栈
UNION:出栈两个集合,然后把两者的并集入栈
INTERSECT:出栈两个集合,然后把二者的交集入栈
ADD:出栈两个集合,然后把先出栈的集合加入到后出栈的集合中,把结果入栈
每次操作后,输出栈顶集合的大小(即元素个数)。例如栈顶元素是A={ {}, {{}} }, 下一个元素是B={ {}, {{{}}} },则:
UNION操作将得到{ {}, {{}}, {{{}}} },输出3.
INTERSECT操作将得到{ {} },输出1
ADD操作将得到{ {}, {{{}}}, { {}, {{}} } },输出3.
(输入:先输入测试次数,再输入操作次数,再输入具体操作)
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
0
0
1
0
0
思路:
给每个集合分配一个编号,在栈中存储编号,注意ADD,UNION和INTERSECTION对栈顶两个集合进行操作,可以提取共性
代码分析可以看紫皮书的栈部分
可以来练习对STL的运用
#include <bits/stdc++.h>using namespace std;typedef set<int> Set; //用Set表示set<int>类型map<set<int>,int> findid;vector<set<int> > match;stack<int> s;int id(Set a) //给每个集合分配id或返回已有Id值{ if(findid.count(a)) return findid[a]; match.push_back(a); return findid[a]=match.size()-1;}int main(){ int T;cin>>T; while(T--) { int n;cin>>n; for(int i=0;i<n;i++) { string str; cin>>str; if(str[0]=='P') s.push(id(Set())); //Set()表示建立一个空集 else if(str[0]=='D') s.push(s.top()); else { Set x; Set x1=match[s.top()]; s.pop(); Set x2=match[s.top()]; s.pop(); if(str[0]=='U') set_union(x1.begin(),x1.end(),x2.begin(),x2.end(),inserter(x,x.begin())); //求并集,放入x else if(str[0]=='I') set_intersection(x1.begin(),x1.end(),x2.begin(),x2.end(),inserter(x,x.begin()));//求交集,放入x else { x2.insert(id(x1)); x=x2; //把包含x1的新集合x2赋给x } s.push(id(x)); //x统一操作 } Set x=match[s.top()]; cout<<x.size()<<endl; } cout<<"***"<<endl; } return 0;}
阅读全文
0 0
- The SetStack Computer集合栈计算器(stack的运用)
- uva 12096 - The SetStack Computer(STL 的运用)
- UVAOJ 12096 The SetStack Computer(STL的运用)
- uva 12096 - The SetStack Computer(STL 的运用)
- UVa12096 The SetStack Computer (stack)
- The SetStack Computer UVA12096(集合栈计算机)
- 集合栈计算机(The SetStack Computer)
- The SetStack Computer(UVa12096&&POJ3121) (集合栈)
- uva 12096 The SetStack Computer (map,set,stack容器)
- UVA12096 The SetStack Computer(栈的应用)
- The SetStack Computer(stack) -uva 12096
- UVa 12096 The SetStack Computer 【STL】【stack】
- UVA - 12096 The SetStack Computer STL的大集合
- UVA 12096 The SetStack Computer(stack及其它STL容器综合使用)
- UVA 12096 The SetStack Computer STL集合集合操作
- 算法竞赛入门经典(第2版)例题5-5 集合栈计算机 (The SetStack Computer UVa 12096)
- 【紫书】(UVa12096) The SetStack Computer
- 12096 - The SetStack Computer
- 选择排序
- 配置Webpack-dev-server+初步了解react中元素、组件、事件、props传递
- 单例模式和多线程
- iOS PDF使用CATiledLayer展示
- 第08讲 mysql添加数据
- The SetStack Computer集合栈计算器(stack的运用)
- Android binder
- 501. Find Mode in Binary Search Tree
- java算法学习笔记--线性表篇
- 7-17数组下
- Struts2学习笔记(1)
- vim中自己容易忘记的命令
- ssh框架注解方式持久层无法注入sessionFactory解决方法
- 第09讲mysql修改数据