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 编码是由大写字母组成的 ...
随机推荐
- iOS-@inerface的11条规范写法
总结一些interface声明时的规范,相关宏的介绍,定义方法时有用的修饰符,编写注释的规范,最终写出一个合格的头文件. 1.读写权限 1.1实例变量的@public,@protected,@priv ...
- Codeforces 1158C Permutation recovery
https://codeforces.com/contest/1158/problem/C 题目 已知 $p_1, p_2, \dots, p_n$ 是 $1$ 到 $n$ 的一个排列. 给出关于这个 ...
- Redis集群_主从配置
链接地址http://www.2cto.com/database/201502/377069.html 收藏备用. Redis主从配置(Master-Slave) 一. Redis Replicati ...
- script error总结
移动端的页面在控制台报出一个script error,通常的原因有一下几点: 1. 脚本引入错误 可能是脚本的地址不对,协议不对(http或https问题),本地host文件绑定的地址不对 2. 方法 ...
- android2.2 watchdog分析
1 watchdog分析 Watchdog就是“看门狗”.其最初存在的意义是因为以前嵌入式设备上的程序经常跑飞(电磁干扰之类的),所以专门设置了一个硬件看门狗,每个一段时间,看门狗就去检查一下某个参数 ...
- Python(2)-- 运算符
1. 算术运算符 常规: 加(+).减(-).乘(*).除(/).取模(%) 此外: 幂(**):返回x的y次幂, eg: 2**3---返回 2 的 5 次幂,输出结果32 取整除(//):返回商的 ...
- jackson 的UnrecognizedPropertyException错误
阅读更多 前段时间,使用jackson封装了json字符串转换为javabean的方法,代码如下: public static <T> T renderJson2Object(String ...
- xCode控制台的使用-应用闪退原因跟踪
xCode控制台的使用-应用闪退原因跟踪 今天遇到这个一个问题,使用xCode version = 5.0 编译一个程序,刷到IOS7设备后,应用运行正常,但是刷新到IOS<7,打开饮用后就会出 ...
- spring一些总结
Spring中三种实例化bean的方法: 1)使用类构造器 <bean id="orderService" class="cn.itcast.OrderServ ...
- msm8917 GPIO Voh(min)
有些 pin 可以當成多種 function, 此例以 GPIO function P3 voltage 為例 Voh(min) = 1.67 - 0.45 = 1.22 V