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混合应用)的更多相关文章

  1. uva 12096 - The SetStack Computer(集合栈)

    例题5-5 集合栈计算机(The Set Stack Computer,ACM/ICPC NWERC 2006,UVa12096) 有一个专门为了集合运算而设计的"集合栈"计算机. ...

  2. uva 12096 The SetStack Computer

    点击打开链接uva 12096 思路: STL模拟 分析: 1 题目给定5种操作,每次输出栈顶集合的元素的个数 2 利用stack和set来模拟,set保存集合的元素.遇到push的时候直接在stac ...

  3. UVa 12096 The SetStack Computer【STL】

    题意:给出一个空的栈,支持集合的操作,求每次操作后,栈顶集合的元素个数 从紫书给的例子 A={{},{{}}} B={{},{{{}}}} A是栈顶元素,A是一个集合,同时作为一个集合的A,它自身里面 ...

  4. uva 12096 The SetStack Computer(STL set的各种库函数 交集 并集 插入迭代器)

    题意: 有5种操作: PUSH:加入“{}”空集合入栈. DUP:栈顶元素再入栈. UNION:出栈两个集合,取并集入栈. INTERSECT:出栈两个集合,取交集入栈. ADD:出栈两个集合,将先出 ...

  5. 12096 - The SetStack Computer UVA

    Background from Wikipedia: \Set theory is a branch of mathematics created principally by the German ...

  6. UVa12096.The SetStack Computer

    题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  7. 集合栈计算机(The SetStack Computer, ACM/ICPC NWERC 2006,Uva12096)

    集合栈计算机(The SetStack Computer, ACM/ICPC NWERC 2006,Uva12096) 题目描述 有一个专门为了集合运算而设计的"集合栈"计算机.该 ...

  8. EOJ 1641/UVa The SetStack Computer

    Background from Wikipedia: “Set theory is a branch of mathematics created principally by the German ...

  9. UVA12096 - The SetStack Computer(set + map映射)

    UVA12096 - The SetStack Computer(set + map映射) 题目链接 题目大意:有五个动作: push : 把一个空集合{}放到栈顶. dup : 把栈顶的集合取出来, ...

随机推荐

  1. 后台可以用layui快速开发

    后台可以用layui快速开发

  2. Java开发工程师(Web方向) - 01.Java Web开发入门 - 第6章.蜂巢

    第6章--蜂巢 蜂巢简介 网站开发完,就需要测试.部署.在服务器上运行. 网易蜂巢: 采用Docker容器化技术的云计算平台 https://c.163.com 容器管理:容器可被视作为云主机的服务器 ...

  3. CSP201609-1:最大波动

    引言:CSP(http://www.cspro.org/lead/application/ccf/login.jsp)是由中国计算机学会(CCF)发起的"计算机职业资格认证"考试, ...

  4. OpenPAI大规模人工智能平台安装部署文档

    环境要求: 如果需要图形界面,需要在Ubuntu系统安装,否则centos系统安装时是没有问题的(web端和命令行进行任务提交) 安装过程需要有另外一台控制端机器(注意:区别于集群所在的任何一台服务器 ...

  5. 深度学习笔记 (二) 在TensorFlow上训练一个多层卷积神经网络

    上一篇笔记主要介绍了卷积神经网络相关的基础知识.在本篇笔记中,将参考TensorFlow官方文档使用mnist数据集,在TensorFlow上训练一个多层卷积神经网络. 下载并导入mnist数据集 首 ...

  6. Python3 Tkinter-OptionMenu

    1.创建 from tkinter import * root=Tk() v=StringVar() v.set('xs') om=OptionMenu(root,v,'Python','PHP',' ...

  7. Spring 3整合Quartz 2实现定时任务:动态添加任务

    先展示一下后台管理定时任务效果图: 1.新增任务页面: 2.列表页(实现任务的禁用启用) 3.数据库脚本: -- ------------------------------ Table struct ...

  8. [leetcode-693-Binary Number with Alternating Bits]

    Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will a ...

  9. LVS+Keepalive+Nginx实现负载均衡

    本文参考:http://blog.csdn.net/yinwenjie/article/details/47211551 简单粗暴写一下,做备忘,刚刚搭好没做优化呢,后期补充 一.机器准备 LVS-M ...

  10. Alpha版——版本控制报告(Thunder)

    Part One 回答问题: 0.在吹牛之前,先回答这个问题:如果你的团队来了一个新队员,有一台全新的机器,你们是否有一个文档,只要设置了相应的权限,她就可以根据文档,从头开始搭建环境,并成功地把最新 ...