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. 安装配置apache sentry服务

    环境 系统环境:Centos6.7 Hadoop版本:CDH5.10 jdk版本:jdk7 注:本文并未集成kerberos组件 安装Sentry Server 选择安装hive的节点进行安装测试: ...

  2. linux系统——机制与策略(二)

    策略与机制 大部分策略与机制的区别定义是,策略是描述如何实现什么功能,机制则是需要实现怎样的功能.在"The Art of Unix Programming" 中Raymond通过 ...

  3. 论文笔记:《OverFeat: Integrated Recognition, Localization and Detection using Convolutional Networks DeepLearning 》

    一.Abstract综述 训练出一个CNN可以同时实现分类,定位和检测..,三个任务共用同一个CNN网络,只是在pool5之后有所不同 二.分类 这里CNN的结构是对ALEXNET做了一些改进,具体的 ...

  4. CDOJ 30 裸最短路 SPFA

    最短路 Edit Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit ...

  5. bzoj 3704 昊昊的机油之GRST 贪心dp,思维

    昊昊的机油之GRST Time Limit: 10 Sec  Memory Limit: 1024 MBSubmit: 80  Solved: 33[Submit][Status][Discuss] ...

  6. java课后作业-4

    一.编写一个方法,使用以上算法生成指定数目(比如1000个)的随机整数. public class suiji { private static final int N = 200; private ...

  7. 微信小程序 报警告的解决办法

    wx:for   如果没有给它相应的  wx:key 控制台就会有警告,解决的办法给它添加相应的key警告就消失啦

  8. jsonp 格式

    jQuery(document).ready(function(){ $.ajax({ type: "get", async: false, url: "http://f ...

  9. bat文件【java调用】

    Runtime.getRuntime().exec("cmd /c del c:\\a.doc");   //Runtime.getRuntime().exec("not ...

  10. duilib入门简明教程 -- VS环境配置(2) (转)

    原文转自:http://www.cnblogs.com/Alberl/p/3342030.html     既然是入门教程,那当然得基础点,因为搜索duilib相关资料时,发现有些小伙伴到处都是编译错 ...