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 : 把栈顶的集合取出来, ...
随机推荐
- POJ 3256 (简单的DFS)
//题意是 K N, M; //有K个牛 N个牧场,M条路 ,有向的 //把K个牛放到任意的n个不同牧场中,问所有牛都可以到达的牧场数的总和 //这是一道简单的DFS题 //k 100 //n 1 ...
- 【springmvc+mybatis项目实战】杰信商贸-3.需求分析与数据库建模
开发步骤需求:生产厂家信息维护基础表FACTORY_C 1.业务需求:a)<需求说明书> 1)描述业务功能 生产厂家模块 功能:为在购销合同模块中的货物信息和附件信 ...
- Office 365 E3功能
本文简要总结了Office 365E3的功能
- 57[LeetCode] Insert Interval
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- AC 自动机——多模式串匹配
网站上的敏感词过滤是怎么实现的呢? 实际上,这些功能最基本的原理就是字符串匹配算法,也就是通过维护一个敏感词的字典,当用户输入一段文字内容后,通过字符串匹配算法来检查用户输入的内容是否包含敏感词. B ...
- C++ 学习笔记之——字符串和字符串流
1. 字符数组 字符数组,也就是存放字符类型数据的数组,只不过字符数组的结尾必须是 '\0'.C++ 已经提供了一些字符串处理函数,这些函数被封装在头文件 和 <string.h> 中. ...
- py3.6+anaconda下安装opencv3
py3.6+anaconda下安装opencv3 首先声明-网上的方法大多数都是有毒的.也不知道给的什么鬼方法都不行. 我说下我的方法.去这个网站https://pypi.tuna.tsinghua. ...
- node.js应用--转载
最近,在向大学生们介绍 HTML5 的时候,我想要对他们进行问卷调查,并向他们显示实时更新的投票结果.鉴于此目的,我决定快速构建一个用于此目的的问卷调查应用程序.我想要一个简单的架构,不需要太多不同的 ...
- HDU 3007 Buried memory(计算几何の最小圆覆盖,模版题)
Problem Description Each person had do something foolish along with his or her growth.But,when he or ...
- 关于14道魔鬼js考题的整理
1.(function(){ return typeof arguments })(); 这里返回时是argument类型,它是个类数组,也就对象,所以是object,准确谁是[object argu ...