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. mysql里制造一个错误

    最近突然想到的,由于在触发器中执行失败事务性表会自动回滚. 所以就想制造一个错误,在群里问了问最后还真得到一个制造错误的方法,或者可以叫做自定义异常 SIGNAL SQLSTATE ' SET MES ...

  2. TortoiseGit保存用户名和密码的方法

    TortoiseGit在提交或者pull时总会提示你输入用户名密码,非常麻烦,那如何解决呢? 1. 对于TortoiseGit 1.8.1.2及其后的版本,右键选择settings ——> Gi ...

  3. 【BZOJ1717】[Usaco2006 Dec]Milk Patterns 产奶的模式 (二分+SA)

    求重复k次的最长重复子串,解法见罗穗骞大神的后缀数组论文 ; var x,y,rank,sa,h,s,num,c:..maxn] of longint; n,time:longint; functio ...

  4. SPOJ - SUBLEX 【后缀自动机】

    题目 求第K小子串 题解 建好SAM后,拓扑排序,反向传递后面所形成的串的数量 最后从根开始,按照儿子形成串的数量与k比较走就好了 #include<iostream> #include& ...

  5. Lis(bzoj 3532)

    Description 给定序列A,序列中的每一项Ai有删除代价Bi和附加属性Ci.请删除若干项,使得4的最长上升子序列长度减少至少1,且付出的代价之和最小,并输出方案.    如果有多种方案,请输出 ...

  6. XWW的难题(bzoj 3698)

    Description XWW是个影响力很大的人,他有很多的追随者.这些追随者都想要加入XWW教成为XWW的教徒.但是这并不容易,需要通过XWW的考核.XWW给你出了这么一个难题:XWW给你一个N*N ...

  7. 最近发的一些csdn下载资源

    原文发布时间为:2009-11-02 -- 来源于本人的百度文章 [由搬家工具导入] http://wjwu1988.download.csdn.net/treeview的datasource类    ...

  8. 阿里巴巴Java开发手册公开版(转)

    1.不要嫌名字长 无论是方法,变量,还是函数的取名,不要嫌弃名称太长,只要能够表示清楚含义就可以了. 2.String[] args而不是String args[] 中括号是数组类型的一部分,数组定义 ...

  9. android中提示&对话框----AlertDialog

    AlertDialog(对话框) 一.对话框的基本使用流程 step1:创建AlertDialog.Buider; step2:调用setIcon()设置图标,setTitle()或者setCusto ...

  10. android中与Adapter相关的控件----GridView

    GridView(网格视图)讲解 一.GridView(网格视图)这个是控件也是比较多,和listView的很多地方都是一样的,但是GridView可以显示多列,而listView只能显示一列,个人觉 ...