UVA12096 - The SetStack Computer(set + map映射)
UVA12096 - The SetStack Computer(set + map映射)
题目大意:有五个动作:
push : 把一个空集合{}放到栈顶。
dup : 把栈顶的集合取出来,在入栈两次。
add : 出栈两次。把第一个集合作为一个元素放入第二个集合中,再将第二个集合入栈
union: 出栈两次,取这两个集合的并集。将结果入栈。
intersect: 出栈两次。取这两个集合的交集,将结果入栈。
每次运行动作后还须要输出眼下栈顶集合的元素个数。
解题思路:这题能够用栈和set来模拟,push就把空的集合入栈,可是在并集和交集的时候就须要判段集合是否同样,所以这里能够用map把出现过的集合手动的映射成数字。
代码:
#include <cstdio>
#include <cstring>
#include <stack>
#include <set>
#include <map>
using namespace std;
char op[15];
int n;
typedef set<int> E;
stack<E> s;
map<E, int> vis;
E tmp1, tmp2;
set<int>::iterator it;
int num;
void hash (E a) {
if (!vis.count(a))
vis[a] = ++num;
}
void Push () {
tmp1.clear();
s.push (tmp1);
}
void Dup () {
tmp1 = s.top();
s.push (tmp1);
}
void Add () {
tmp2 = s.top();
s.pop();
tmp1 = s.top();
s.pop();
tmp1.insert (vis[tmp2]);
hash(tmp1);
s.push(tmp1);
}
void Union () {
tmp2 = s.top();
s.pop();
tmp1 = s.top();
s.pop();
for (it = tmp1.begin(); it != tmp1.end(); it++)
tmp2.insert (*it);
hash (tmp2);
s.push (tmp2);
}
void Intersect () {
tmp2 = s.top();
s.pop();
tmp1 = s.top();
s.pop();
E tmp;
for (it = tmp1.begin(); it != tmp1.end(); it++)
if (tmp2.count(*it))
tmp.insert (*it);
hash (tmp);
s.push(tmp);
}
void solve () {
switch (op[0]) {
case 'P' : Push(); break;
case 'D' : Dup(); break;
case 'U' : Union(); break;
case 'I' : Intersect(); break;
case 'A' : Add(); break;
}
printf ("%d\n", s.top().size());
}
void init () {
num = 1;
while (!s.empty()) {
s.pop();
}
vis.clear();
}
int main () {
int T;
scanf ("%d", &T);
while (T--) {
scanf ("%d", &n);
while (n--) {
scanf ("%s", op);
solve();
}
printf ("***\n");
}
return 0;
}
UVA12096 - The SetStack Computer(set + map映射)的更多相关文章
- UVa12096.The SetStack Computer
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- uva12096 The SetStack Computer By sixleaves
代码 typedef map<Set, vector<Set> Setcache; stack< ci ...
- 集合栈计算机(The SetStack Computer, ACM/ICPC NWERC 2006,Uva12096)
集合栈计算机(The SetStack Computer, ACM/ICPC NWERC 2006,Uva12096) 题目描述 有一个专门为了集合运算而设计的"集合栈"计算机.该 ...
- 12096 - The SetStack Computer UVA
Background from Wikipedia: \Set theory is a branch of mathematics created principally by the German ...
- UVA.12096 The SetStack Computer ( 好题 栈 STL混合应用)
UVA.12096 The SetStack Computer ( 好题 栈 STL混合应用) 题意分析 绝对的好题. 先说做完此题的收获: 1.对数据结构又有了宏观的上的认识; 2.熟悉了常用STL ...
- EOJ 1641/UVa The SetStack Computer
Background from Wikipedia: “Set theory is a branch of mathematics created principally by the German ...
- ZOJ 3644 Kitty's Game dfs,记忆化搜索,map映射 难度:2
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4834 从点1出发,假设现在在i,点数为sta,则下一步的点数必然不能是sta的 ...
- POJ2503——Babelfish(map映射+string字符串)
Babelfish DescriptionYou have just moved from Waterloo to a big city. The people here speak an incom ...
- map——映射(message.cpp)
信息交换 (message.cpp) [题目描述] Byteland战火又起,农夫John派他的奶牛潜入敌国获取情报信息. Cow历尽千辛万苦终于将敌国的编码规则总结如下: 1 编码是由大写字母组成的 ...
随机推荐
- tcp协议 tcpip协议 http协议,IP,DNS,端口号
每当看到HTTP协议,tcp/ip协议,TCP协议总是蒙圈:在这里先记录一下,方面自己查看 TCP协议:TCP(Transmission Control Protocol 传输控制协议)是一种面向连接 ...
- Bolzano-Weierstrass 定理
这个定理是从吴崇试老师的数学物理方法课里看到的,表述如下: 有界的无穷(复数)序列至少有一个聚点. 序列的聚点定义为 给定序列 $\{z_n\}$,若存在复数 $z$,对于任意给定的 $\vareps ...
- [SCOI2007][bzoj1070] 修车 [费用流]
题面 传送门 思路 我们考虑某个工人修车的从前到后序列如下: ${W_1,W_2,W_3,...,W_n}$ 那么,对于这n辆车的车主而言,他们等候的总时间为: $\sum_{i=1}^{n}W_i\ ...
- linux系统——hosts文件修改
1. 关于/etc/host,主机名和IP配置文件 Hosts - The static table lookup for host name(主机名查询静态表) Linux 的/etc/hosts是 ...
- 移动端px转rem的两种方法
rem使用方法: rem ,root element,即相对于根元素的大小,浏览器默认字符大小为16px,此时1rem相当于16px. 方法1 设置font-size: body{font-size ...
- ios - 工具类
这几天看项目,把俺旁边小哥哥的一个工具类相中了,希望对大家有所帮助哦~~~~~~~~~ // // PLZ_Tool.h // // Created by penglaizhi on 2017/7/3 ...
- Linux下常用的命令记录
本文章记录我在linux系统下常用或有用的系统级命令,包括软硬件查看.修改命令,有CPU.内存.硬盘.网络.系统管理等命令.但本文不打算介绍生僻命令,也不介绍各个linux发行版下的特有命令,且以后会 ...
- COGS 2485. [HZOI 2016]从零开始的序列
2485. [HZOI 2016]从零开始的序列 ★★ 输入文件:sky_seq.in 输出文件:sky_seq.out 简单对比时间限制:1 s 内存限制:256 MB [题目描述] ...
- 用.net开发wap
原文发布时间为:2010-08-19 -- 来源于本人的百度文章 [由搬家工具导入] .NET 模板:http://download.csdn.net/source/2631001 WAP应用程序结构 ...
- 从头开始写项目Makefile(十):make内嵌函数及make命令显示【转】
转自:http://blog.csdn.net/shallnet/article/details/38314473#comments 版权声明:本文为博主原创文章,未经博主允许不得转载.如果您觉得文章 ...