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

13916058 12096 The SetStack Computer Accepted C++ 0.302 2014-07-21 03:43:15

The SetStack Computer

Background from Wikipedia: \Set theory is a branch of mathematics created principally by the German mathematician Georg Cantor at the end of the 19th century. Initially controversial, set theory has come to play the role of a foundational theory in modern mathematics, in the sense of a theory invoked to justify assumptions made in mathemat ics concerning the existence of mathematical objects
(such as numbers or functions) and their properties.
Formal versions of set theory also have a founda
tional role to play as specifying a theoretical ideal
of mathematical rigor in proofs."
Given this importance of sets, being the basis of mathematics, a set of eccentric theorist set off to
construct a supercomputer operating on sets instead of numbers. The initial SetStack Alpha is unde
construction, and they need you to simulate it in order to verify the operation of the prototype.
The computer operates on a single stack of sets, which is initially empty. After each operation, the
cardinality of the topmost set on the stack is output. The cardinality of a set S is denoted jSj and is the
number of elements in S. The instruction set of the SetStack Alpha is PUSH, DUP, UNION, INTERSECT
and ADD.
PUSH will push the empty set fg on the stack.
DUP will duplicate the topmost set (pop the stack, and then push that set on the stack twice).
UNION will pop the stack twice and then push the union of the two sets on the stack.
INTERSECT will pop the stack twice and then push the intersection of the two sets on the stack.
ADD will pop the stack twice, add the rst set to the second one, and then push the resulting se
on the stack.
For illustration purposes, assume that the topmost element of the stack is
A = ffg; ffggg
and that the next one is
B = ffg; fffgggg
For these sets, we have jAj = 2 and jBj = 2. Then:
UNION would result in the set ffg, ffgg, fffgggg. The output is 3.
INTERSECT would result in the set ffgg. The output is 1.
ADD would result in the set ffg, fffggg, ffg,ffgggg. The output is 3.
Input
An integer 0 T 5 on the rst line gives the cardinality of the set of test cases. The rst line of each
test case contains the number of operations 0 N 2000. Then follow N lines each containing one o
the ve commands. It is guaranteed that the SetStack computer can execute all the commands in the
sequence without ever popping an empty stack.
Output
For each operation specied in the input, there will be one line of output consisting of a single integer
This integer is the cardinality of the topmost element of the stack after the corresponding command
has executed. After each test case there will be a line with `***' (three asterisks).
Sample Input
2
9
PUSH
DUP
ADD
PUSH
ADD
DUP
ADD
DUP
UNION
5
PUSH
PUSH
ADD
PUSH
INTERSECT
Sample Output
0
0
1
0
1
1
2
2
2
***
0
0
1
0
0
***


解题思路:模拟五个操作即可。读题十分重要。还有通过此题,可以增加使用set容器的熟练程度。

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <stack>
  5. #include <map>
  6. #include <set>
  7. using namespace std;
  8. const int MAXN = ;
  9. const int N = ;
  10.  
  11. int cnt;
  12. stack<set<int> > stk;
  13. map<set<int>, int> mp;
  14. set<int> s1, s2;
  15.  
  16. void pop() {
  17. s1 = stk.top();
  18. stk.pop();
  19. s2 = stk.top();
  20. stk.pop();
  21. }
  22.  
  23. void Push() {
  24. set<int> s;
  25. stk.push(s);
  26. printf("0\n");
  27. }
  28.  
  29. void Dup() {
  30. set<int> s;
  31. s = stk.top();
  32. stk.push(s);
  33. printf("%d\n", s.size());
  34. }
  35.  
  36. void Union() {
  37. pop();
  38. set<int>::iterator it;
  39. for (it = s1.begin(); it != s1.end(); it++)
  40. s2.insert(*it);
  41. stk.push(s2);
  42. printf("%d\n", s2.size());
  43. }
  44.  
  45. void Intersect() {
  46. pop();
  47. set<int> s3;
  48. set<int>::iterator it;
  49. for (it = s1.begin(); it != s1.end(); it++)
  50. if (s2.find(*it) != s2.end())
  51. s3.insert(*it);
  52. stk.push(s3);
  53. printf("%d\n", s3.size());
  54. }
  55.  
  56. void Add() {
  57. pop();
  58. if (s1.empty())
  59. s2.insert();
  60. else {
  61. if (!mp[s1])
  62. mp[s1] = cnt++;
  63. s2.insert(mp[s1]);
  64. }
  65. stk.push(s2);
  66. printf("%d\n", s2.size());
  67. }
  68.  
  69. int main() {
  70. int t, n;
  71. string str;
  72. cin >> t;
  73. while ( t --) {
  74. cin >> n;
  75. while (!stk.empty())
  76. stk.pop();
  77. cnt = MAXN;
  78. mp.clear();
  79. while (n--) {
  80. cin >> str;
  81. if (str[] == 'P')
  82. Push();
  83. else if (str[] == 'D')
  84. Dup();
  85. else if (str[] == 'U')
  86. Union();
  87. else if (str[] == 'I')
  88. Intersect();
  89. else Add();
  90. }
  91. printf("***\n");
  92. }
  93. return ;
  94. }

UVa12096.The SetStack Computer的更多相关文章

  1. UVA12096 - The SetStack Computer(set + map映射)

    UVA12096 - The SetStack Computer(set + map映射) 题目链接 题目大意:有五个动作: push : 把一个空集合{}放到栈顶. dup : 把栈顶的集合取出来, ...

  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. 集合栈计算机 (The SetStack Computer,ACM/ICPC NWERC 2006,UVa12096

    题目描述: #include<iostream> #include<string> #include<set> #include<map> #inclu ...

  8. 【紫书】(UVa12096) The SetStack Computer

    突然转进到第五章的low题目的原因是做到图论了(紫书),然后惊喜的发现第一题就做不出来.那么里面用到了这一题的思想,我们就先解决这题.当然,dp必须继续做下去,这是基本功.断不得. 题意分析 这条题真 ...

  9. uva 12096 - The SetStack Computer(集合栈)

    例题5-5 集合栈计算机(The Set Stack Computer,ACM/ICPC NWERC 2006,UVa12096) 有一个专门为了集合运算而设计的"集合栈"计算机. ...

随机推荐

  1. Linux磁盘及文件系统管理 2---- 使用fdisk进行磁盘管理

    1 FDISK分区工具 1 fsidk是来自IBM的分区工具,支持绝大多数的操作系统,几乎所有的Linux都装有fdisk 2 fdisk是一个支持MBR的分区工具,如果要使用GPT的话我们无法使用f ...

  2. JavaScript 获取CSS媒体查询信息

    var result = window.matchMedia('(max-width: 700px)'); if (result.matches) { console.log('页面宽度小于等于700 ...

  3. JavaScript总结(二) 系统分析

    学习Javascript的时候.遇到了一个问题. 看了牛腩的视频,感觉讲的太简单和基础,像认识一位新朋友.但仅有一面之缘就结束了. 再看姜浩的视频.又看得迷迷糊糊,让我找到了当初看两位台湾专家讲VB. ...

  4. C#用注册表开机自动启动某某软件

    代码如下: public static void chkAutoRun(bool isRun) { if (isRun)//开机自动启动 { try { RegistryKey runKey = Re ...

  5. iOS会议和组织

    全世界有许多iOS会议和组织,如果你没有机会去参加,知道他们的存在和向他们学习对你也是有益的.事实上,他们中有些提供免费的幻灯片.视频,有用资料等,所以你不能够忽视他们. 有一些会议的主题并不仅仅关于 ...

  6. redis的5种数据结构的简介

    5种数据结构 1.字符串 Redis 字符串是一个字节序列.在 Redis 中字符串是二进制安全的,这意味着它们没有任何特殊终端字符来确定长度,所以可以存储任何长度为 512 兆的字符串. 示例 12 ...

  7. Memcached的一些知识

    一.内存分配在Memcached内存结构中有两个非常重要的概念:slab 和 chunk,我们先从下图中对这两个概念有一个感性的认识: memcached内存结构Slab是一个内存块,它是memcac ...

  8. arm汇编(c内嵌汇编及c和汇编互调)

    C语言编译成汇编: arm-linux-gcc -S test.c -o test.S C语言编译成可执行文件: arm-linux-gcc test.c -o test 多个文件编译链接: arm- ...

  9. (转) Resource file and Source file

    基本上是这样的,Sourcefile文件夹里面放的是CPP文件这些,Resourcefile文件夹是资源文件夹,里面可以放你程序里需要的资源,包括图标,对话框,图片等等:对应的文件如下: Source ...

  10. Mysql学习(慕课学习笔记5)约束

    约束类型: 1.NOT NULL (非空约束) 2.PRIMARY KEY(主键约束) 每张数据表只能存在一个主键 主键保证记录的唯一性 主键自动为NOT NULL (Auto_increment  ...