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,它自身里面 ...
随机推荐
- Linux vi 中搜索关键字
当你用vi打开一个文件后,因为文件太长,如何才能找到你所要查找的关键字呢? 在vi里可没有菜单-〉查找 不过没关系,可以在命令模式下敲斜杆( / )这时在状态栏(也就是屏幕左下脚)就出现了 “/” 然 ...
- PyQt4学习笔记2:事件和信号
事件是任何 GUI 程序中很重要的部分.所有 GUI 应用都是事件驱动的.一个应用对其生命期产生的不同的事件类型做出反应.事件是主要由应用的用户产生.但是,也可以通过其他方法产生,比如,网络通信,窗口 ...
- NULL, NUL, EOF, '\0',0区别
NULL: 定义为0或0L或(void *)0,用于指示一个指针值是空,即什么都不指:'\0': 用且只用字符串结束符;NUL : 0x00,0值字符,可以用于结束ASCII字符串,和'\0'类似,但 ...
- 2、[转]WPF与WinForm的比较
http://www.cnblogs.com/KnightsWarrior/archive/2010/07/09/1774059.htmlhttp://www.cnblogs.com/zenghong ...
- Kinetic使用注意点--ellipse
new Ellipse(config) 参数: config:包含所有配置项的对象. { radius: "半径,可以用数字a.数组[a,b]或对象{x:a,y:b}来表示" } ...
- PHP - PDO 之 mysql 基础操作
<?php /* pdo 学习 */ $dsn = 'mysql:host=localhost;dbname=cswl';//构建连接dsn $db = new pdo($dsn,'root', ...
- 微软职位内部推荐-SDE II-MODC-Beijing
微软近期Open的职位: JOB TITLE: Software Design Engineer IIDEPARTMENT: Microsoft Office Division ChinaIMMEDI ...
- 第一个js面向对象的小实验
$.extend({ cal: function (num1,num2,oper,aftercal) { this.n1 = num1; ...
- 前端跨域之html5 XMLHttpRequest Level2
前端代码 var xhr=new XMLHttpRequest(); xhr.open('POST','http://127.0.0.1:8081/ceshi',true); xhr.onreadys ...
- SQL优化之索引
最近碰到一个问题,因数据量越来越大,然后存储过程查询过慢!后来发现没有加索引列导致的!从这里让我开始慢慢去了解索引的原理及作用!以下是我的总结,个人理解只供参考: SQL SERVER提供了两种索引: ...