UVA.12096 The SetStack Computer ( 好题 栈 STL混合应用)
UVA.12096 The SetStack Computer ( 好题 栈 STL混合应用)
题意分析
绝对的好题。
先说做完此题的收获:
1.对数据结构又有了宏观的上的认识;
2.熟悉了常用STL(set,map,vector)的常用用法;
3.学习了一种问题转化的方式。
我想如果告诉我这题用STL解决,并且还能独立完成的话,那么STL应该算是过关了。
有下列操作集:
1.PUSH:向栈顶PUSH一个空集合的元素;
2.DUP: 弹出栈顶元素;
3.UNION: 弹出2个栈顶元素,并且去并集后入栈;
4.INTERSECT:弹出2个栈顶元素,去交集后入栈;
5.ADD:弹出2个栈顶元素,将第一个元素表示的集合加入到第二个元素中。
每次执行完操作,要输出当前栈顶元素中的集合个数,若为空集,则输出0。
我的第一感觉是set类型的stack,但是由于对STL这方面的不熟悉,没有着手实现,而是直接参考了lrj大大的写法。
未完:有时间一定补上题解!!
代码总览
#include <iostream>
#include <set>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <stack>
#include <vector>
using namespace std;
typedef map<set<int>,int> Map;
typedef set<int> Set;
vector<Set> IDstore;
Map IDmap;
int findid(Set t)
{
if(IDmap.count(t) == 1) return IDmap[t];
IDstore.push_back(t);
return IDmap[t] = IDstore.size() -1;
}
int main()
{
//freopen("in.txt","r",stdin);
int t;
cin>>t;
while(t--){
stack<int> s;
int n;
string ord;
cin>>n;
while(n--){
cin>>ord;
if(ord[0] == 'P'){
s.push(findid(Set()));
}else if(ord[0] == 'D'){
s.push(s.top());
}else{
Set t1 = IDstore[s.top()];s.pop();
Set t2 = IDstore[s.top()];s.pop();
Set t;
if(ord[0] == 'U'){
set_union(t1.begin(),t1.end(),t2.begin(),t2.end(),inserter(t,t.begin()));
}else if(ord[0] == 'I'){
set_intersection(t1.begin(),t1.end(),t2.begin(),t2.end(),inserter(t,t.begin()));
}else if(ord[0] == 'A'){
t = t2;
t.insert(findid(t1));
}
s.push(findid(t));
}
cout<<IDstore[s.top()].size()<<endl;
}
cout<<"***"<<endl;
}
return 0;
}
UVA.12096 The SetStack Computer ( 好题 栈 STL混合应用)的更多相关文章
- uva 12096 - The SetStack Computer(集合栈)
例题5-5 集合栈计算机(The Set Stack Computer,ACM/ICPC NWERC 2006,UVa12096) 有一个专门为了集合运算而设计的"集合栈"计算机. ...
- uva 12096 The SetStack Computer
点击打开链接uva 12096 思路: STL模拟 分析: 1 题目给定5种操作,每次输出栈顶集合的元素的个数 2 利用stack和set来模拟,set保存集合的元素.遇到push的时候直接在stac ...
- UVa 12096 The SetStack Computer【STL】
题意:给出一个空的栈,支持集合的操作,求每次操作后,栈顶集合的元素个数 从紫书给的例子 A={{},{{}}} B={{},{{{}}}} A是栈顶元素,A是一个集合,同时作为一个集合的A,它自身里面 ...
- uva 12096 The SetStack Computer(STL set的各种库函数 交集 并集 插入迭代器)
题意: 有5种操作: PUSH:加入“{}”空集合入栈. DUP:栈顶元素再入栈. UNION:出栈两个集合,取并集入栈. INTERSECT:出栈两个集合,取交集入栈. ADD:出栈两个集合,将先出 ...
- 12096 - The SetStack Computer UVA
Background from Wikipedia: \Set theory is a branch of mathematics created principally by the German ...
- UVa12096.The SetStack Computer
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- 集合栈计算机(The SetStack Computer, ACM/ICPC NWERC 2006,Uva12096)
集合栈计算机(The SetStack Computer, ACM/ICPC NWERC 2006,Uva12096) 题目描述 有一个专门为了集合运算而设计的"集合栈"计算机.该 ...
- EOJ 1641/UVa The SetStack Computer
Background from Wikipedia: “Set theory is a branch of mathematics created principally by the German ...
- UVA12096 - The SetStack Computer(set + map映射)
UVA12096 - The SetStack Computer(set + map映射) 题目链接 题目大意:有五个动作: push : 把一个空集合{}放到栈顶. dup : 把栈顶的集合取出来, ...
随机推荐
- myeclipse tomcat部署按钮点击没反应
进入workspace目录,删除.metadata\.plugins\org.eclipse.core.runtime\.settings\com.genuitec.eclipse.ast.deplo ...
- (原) MaterialEditor部- UmateriaEditor中 Node编译过程和使用(3)修正
@author: 白袍小道 转载说明原处,爱护劳动 插件同步在GITHUB: DaoZhang_XDZ 说明 1.本篇是接着-----(原) MaterialEditor部- Umat ...
- Flex 布局浅析
除了 CSS 中传统的布局系统之外,CSS3还提供了一个新布局系统.在这个新的框模型中,框的子代采用水平或垂直布局,而且可将未使用的空间分配给特定的子代,或者通过“弹性”分配给应展开的子代,在各子代间 ...
- 通过流的方式操作hadoop的API
通过流的方式操作hadoop的API 功能: 可以直接用来操作hadoop的文件系统 可以用在mapreduce的outputformat中设置RecordWrite 参考: 概念理解 http:// ...
- Matlab提速方法
1. 向量化. 尽量少用for循环. 2. 循环竖着走比横着走快. 3. 内置函数也有优化的空间 不少内置函数都有大量的error check.直接用profiler找出真正干活的.不少内置函数在网上 ...
- Kali渗透测试工具-nslookup
1.交互模式 终端输入nslookup进入交互模式 (1)查询A地址记录(默认) set q=a A记录简单理解将域名转换成对应的IP地址 (2)查询mail exchanger set q=mx m ...
- IntelliJ IDEA 2018 for MAC安装及破解
---------------------说在前面-------------------------- IntelliJ IDEA 2018 版本为2018.1.4 教程按照下载安装sdk.破解两部分 ...
- Netcore logging config
- (转)Android SearchView 搜索框
如果对这个效果感觉不错, 请往下看. 背景: 天气预报app, 本地数据库存储70个大中城市的基本信息, 根据用户输入的或通过搜索框选取的城市, 点击查询按钮后, 异步请求国家气象局数据, 得到返回的 ...
- SQL 单表分页存储过程和单表多字段排序和任意字段分页存储过程
第一种:单表多字段排序分页存储过程 --支持单表多字段查询,多字段排序 create PROCEDURE [dbo].[UP_GetByPageFiledOrder] ( ), --表 ...