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. angular-使用定时器调后台接口

    今天写了一个功能,一个是在两个页面中每隔一秒就调用一个后台接口 首先,这个功能使用了JS里的定时器.JS计时器分为一次性计时器和间隔性触发计时器,此次每隔一秒要调用这个接口,使用的是间隔性触发计时器 ...

  2. TW实习日记:第24-25天

    项目的交付期是真的赶...一直在不断地修改一些小bug,然后消息推送功能出了一个问题,就是不知道为什么PC端会发送两次消息到移动端后台.其中第一条正常第二条会有乱码不正常,可以说是很奇怪了,一开始都认 ...

  3. Java集合学习--集合总结

    一.ArrayList与Vector ArrayList与Vector很多地方大同小异,Vector现在已经基本不再使用.具体的管理如下:1.ArrayList与Vector都实现了List接口,底层 ...

  4. leetcode-帕斯卡三角形

    帕斯卡三角形 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 示例: 输入: 5 输出: [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4 ...

  5. LeetCode - 13. Roman to Integer - 思考if-else与switch的比较 - ( C++ ) - 解题报告

    1.题目: 原题:Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range ...

  6. Mybatis中resultMap与resultType区别

    MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap,resultType是直接表示返回类型的,而resultMap则是对外部ResultM ...

  7. 正确使用memset

    今天做了一道素数打表的题我在使用一个数组记录是否为素数的时候使用了memset,将数组里面的数都清为1,代表是素数,不是素数,就改成0,我在判断这一个数是否为素数是依据也是是0还是1,结果一直存在问题 ...

  8. 20172330 2017-2018-1 《Java程序设计》第九周学习总结

    20172330 2017-2018-1 <程序设计与数据结构>第九周学习总结 教材学习内容总结 本周的学习包括两章内容,分别为异常和递归. 异常 错误和异常都是对象,代表非正常情况或者无 ...

  9. <Effective C++>读书摘要--Templates and Generic Programming<一>

    1.The initial motivation for C++ templates was straightforward: to make it possible to create type-s ...

  10. 3dContactPointAnnotationTool开发日志(十九)

      增加了输出接触点信息到文件功能.