UVa 12096 (STL) The SetStack Computer
题意:
有一个集合栈计算机,栈中的元素全部是集合,还有一些相关的操作。输出每次操作后栈顶集合元素的个数。
分析:
这个题感觉有点抽象,集合还能套集合,倒是和题中配的套娃那个图很贴切。
把集合映射成ID,就可以用 stack<int>来模拟题中的集合栈,然后用 vector<Set> 来根据下标进行集合的索引。
代码虽短,但还须多体会。
#include <cstdio>
#include <string>
#include <vector>
#include <stack>
#include <set>
#include <map>
#include <iostream>
#include <algorithm>
using namespace std; typedef set<int> Set;
map<Set, int> IDcache;
vector<Set> Setcache; #define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin()) int ID(Set x)
{
if(IDcache.count(x)) return IDcache[x];
Setcache.push_back(x);
return IDcache[x] = Setcache.size() - ;
} int main()
{
//freopen("in.txt", "r", stdin); int T;
scanf("%d", &T);
while(T--)
{
stack<int> s;
int n;
scanf("%d", &n);
for(int i = ; i < n; ++i)
{
string op;
cin >> op;
if(op[] == 'P') s.push(ID(Set()));
else if(op[] == 'D') s.push(s.top());
else
{
Set x1 = Setcache[s.top()]; s.pop();
Set x2 = Setcache[s.top()]; s.pop();
Set x;
if(op[] == 'U') set_union(ALL(x1), ALL(x2), INS(x));
if(op[] == 'I') set_intersection(ALL(x1), ALL(x2), INS(x));
if(op[] == 'A') { x = x2; x.insert(ID(x1)); }
s.push(ID(x));
}
printf("%d\n", Setcache[s.top()].size());
} puts("***");
} return ;
}
代码君
UVa 12096 (STL) The SetStack Computer的更多相关文章
- 【例题5-5 UVA 12096 】The SetStack Computer
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 用set来解决这个问题. 考虑如何表示 { {{}} }这个集合 我们可以把{}这个集合和一个数字映射->1 然后把1加入到某 ...
- UVA.12096 The SetStack Computer ( 好题 栈 STL混合应用)
UVA.12096 The SetStack Computer ( 好题 栈 STL混合应用) 题意分析 绝对的好题. 先说做完此题的收获: 1.对数据结构又有了宏观的上的认识; 2.熟悉了常用STL ...
- uva 12096 The SetStack Computer
点击打开链接uva 12096 思路: STL模拟 分析: 1 题目给定5种操作,每次输出栈顶集合的元素的个数 2 利用stack和set来模拟,set保存集合的元素.遇到push的时候直接在stac ...
- 12096 - The SetStack Computer UVA
Background from Wikipedia: \Set theory is a branch of mathematics created principally by the German ...
- 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
题目链接: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) 题目描述 有一个专门为了集合运算而设计的"集合栈"计算机.该 ...
- UVA12096 - The SetStack Computer(set + map映射)
UVA12096 - The SetStack Computer(set + map映射) 题目链接 题目大意:有五个动作: push : 把一个空集合{}放到栈顶. dup : 把栈顶的集合取出来, ...
- UVa 12096 The SetStack Computer【STL】
题意:给出一个空的栈,支持集合的操作,求每次操作后,栈顶集合的元素个数 从紫书给的例子 A={{},{{}}} B={{},{{{}}}} A是栈顶元素,A是一个集合,同时作为一个集合的A,它自身里面 ...
随机推荐
- nrm —— 快速切换 NPM 源 (附带测速功能)
以前我们介绍过cnpmjs.org和最近推出的淘宝 npm 两个 NPM 镜像.除此之外,还有一些国外的 NPM 镜像.不同地区访问不同的镜像速度可能有差异,然后各个镜像各自都可能有少数包暂时没有同步 ...
- JavaScript的常见事件和Ajax小结
一.常见事件类型 1.鼠标事件 事件名称 说明 onclick 鼠标单击时触发 ondbclick 鼠标双击时触发 onmousedown 鼠标左键按下时触发 onmouseup 鼠标释放时触发 on ...
- Python入门二:函数
一.函数的定义和使用 1.基本结构: def 函数名(参数): """ 文档字符串 """ 函数体 返回值 2.函数名: 和变量名命名规则一 ...
- C#向C++编写的DLL传递字符串参数的办法
使用StringBuilder,举例: C++代码中函数定义如下: PVPOWERFORCASTDLL_API int PVPowerForcast(SForcastInfo &_Forcas ...
- Asp.net MVC 如何向webform一样在IIS里添加虚拟目录
相信很多用webform的程序猿都习惯性的使用虚拟目录的形式来对一个程序添加新的功能,那么在mvc下该如何来弄呢? 首先得有一个项目基层的项目,然后我们在这个项目的基础上新增一个功能模块,例如信息发布 ...
- Qt单元测试
单元测试之作用要完成测试用例,保证设计上的耦合依赖通过测试用例,保证覆盖率,提高程序质量 QTest一些有用的静态函数QTest::qExecQTest::qSleepQTest::qWait 例 ...
- linux驱动系列之s3c2440内存布局
刚开始学习linux在2440上面 linux内核分配标志可以分为三类:行为修饰符.区修饰符.类型. 区修饰符表示从哪儿分配内存,内核把物理内存分为多个区,每个区用于不同的目的. 内存中缓冲区存在的原 ...
- Duilib介绍以及各个类的简介
转自http://note.sdo.com/u/icez/n/mvO-X~jyVnpFnM01A0000m DirectUI意为直接在父窗口上绘图(Paint on parent dc directl ...
- MySQL Replication 常用架构
转自: http://www.cnblogs.com/ggjucheng/archive/2012/11/13/2768879.html 前言 MySQLReplicaion本身是一个比较简单的架构, ...
- linux命令useradd添加用户详解
1.作用 useradd或adduser命令用来建立用户帐号和创建用户的起始目录,使用权限是超级用户. 2.格式 useradd [-d home] [-s shell] [-c comment] [ ...