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映射)的更多相关文章

  1. UVa12096.The SetStack Computer

    题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  2. uva12096 The SetStack Computer By sixleaves

    代码     typedef  map<Set,  vector<Set> Setcache;                  stack<               ci ...

  3. 集合栈计算机(The SetStack Computer, ACM/ICPC NWERC 2006,Uva12096)

    集合栈计算机(The SetStack Computer, ACM/ICPC NWERC 2006,Uva12096) 题目描述 有一个专门为了集合运算而设计的"集合栈"计算机.该 ...

  4. 12096 - The SetStack Computer UVA

    Background from Wikipedia: \Set theory is a branch of mathematics created principally by the German ...

  5. UVA.12096 The SetStack Computer ( 好题 栈 STL混合应用)

    UVA.12096 The SetStack Computer ( 好题 栈 STL混合应用) 题意分析 绝对的好题. 先说做完此题的收获: 1.对数据结构又有了宏观的上的认识; 2.熟悉了常用STL ...

  6. EOJ 1641/UVa The SetStack Computer

    Background from Wikipedia: “Set theory is a branch of mathematics created principally by the German ...

  7. ZOJ 3644 Kitty's Game dfs,记忆化搜索,map映射 难度:2

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4834 从点1出发,假设现在在i,点数为sta,则下一步的点数必然不能是sta的 ...

  8. POJ2503——Babelfish(map映射+string字符串)

    Babelfish DescriptionYou have just moved from Waterloo to a big city. The people here speak an incom ...

  9. map——映射(message.cpp)

    信息交换 (message.cpp) [题目描述] Byteland战火又起,农夫John派他的奶牛潜入敌国获取情报信息. Cow历尽千辛万苦终于将敌国的编码规则总结如下: 1 编码是由大写字母组成的 ...

随机推荐

  1. POJ3678 Katu Puzzle 【2-sat】

    题目 Katu Puzzle is presented as a directed graph G(V, E) with each edge e(a, b) labeled by a boolean ...

  2. DocumentFragment批量操作dom

    DocumentFragment,文档片段,不属于文档树,其parentNode为null.当把一个DocumentFragment节点插入文档树时,插入的不是DocumentFragment自身,而 ...

  3. iOS-读取txt文件中文乱码

    一.情景描述: 后台给一个txt文件,编码是utf-8,在Mac电脑Xcode开发环境下读取txt文件内容,汉字会出现乱码,英文没有乱码这种情况. 二.尝试解决方法: 修改编码格式,尝试了NSUTF1 ...

  4. C++ qsort() 函数调用时实参与形参不兼容的问题解决

    <剑指OFFER>刷题笔记 —— 扑克牌顺子 LL今天心情特别好,因为他去买了一副扑克牌,发现里面居然有2个大王,2个小王(一副牌原本是54张^_^)...他随机从中抽出了5张牌,想测测自 ...

  5. sql中的like和正则的区别

    like,模糊查询,更多的是用于匹配已知的字符,比如查询该字段含有1的记录,like ‘%1%’:但是如果要匹配不确定的,但是一个系列的字符,比如数字,最好用regexpselect * from t ...

  6. pat 团体天梯赛 L1-039. 古风排版

    L1-039. 古风排版 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 中国的古人写文字,是从右向左竖向排版的.本题就请你编写 ...

  7. 判断是手机端还是pc端

    <script type="text/javascript"> if (window.location.toString().indexOf('pref=padinde ...

  8. ThickBox弹出框的使用方法

    原文发布时间为:2009-08-22 -- 来源于本人的百度文章 [由搬家工具导入] 请访问:http://www.blueidea.com/articleimg/2007/12/5182/tickb ...

  9. 《Linux命令行与shell脚本编程大全 第3版》Linux命令行---53

    以下为阅读<Linux命令行与shell脚本编程大全 第3版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下:

  10. Codeforces 最大流 费用流

    这套题目做完后,一定要反复的看! 代码经常出现的几个问题: 本机测试超时: 1.init函数忘记写. 2.addedge函数写成add函数. 3.边连错了. 代码TLE: 1.前向星边数组开小. 2. ...