【例题5-5 UVA 12096 】The SetStack Computer
【链接】 我是链接,点我呀:)
【题意】
在这里输入题意
【题解】
用set来解决这个问题。
考虑如何表示
{ {{}} }这个集合
我们可以把{}这个集合和一个数字映射->1
然后把1加入到某个set里面去
即
{1}
则这就对应了->{ {} }
然后把{1}也用一个int对应->2
然后把2加入一个集合
{2}
则这个就对应了->{ {{}} }
这样,每个集合都能用set来表示了.
则用map ,int>来搞映射。
然后正常地用set求交集、并集就好了。
->交集并集set都有现成的函数的。
栈里面可以只存这个set的映射就行。
【错的次数】
在这里输入错的次数
【反思】
这种一层套一层的方法好巧妙。。
【代码】
#include <bits/stdc++.h>
using namespace std;
typedef set<int> Set;
map <Set, int> mymap;
stack <int> sta;
vector <Set> what;
int ID(Set x)
{
if (mymap.find(x) != mymap.end()) return mymap[x];
what.push_back(x);
mymap[x] = (int)what.size() - 1;
return mymap[x];
}
int main()
{
//freopen("F:\\rush.txt", "r", stdin);
ios::sync_with_stdio(0), cin.tie(0);
int T;
cin >> T;
while (T--)
{
what.clear();
while (!sta.empty()) sta.pop();
mymap.clear();
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
string s;
cin >> s;
if (s[0] == 'P')
sta.push(ID(Set()));
else
if (s[0] == 'D')
sta.push(sta.top());
else
{
Set x = what[sta.top()]; sta.pop();
Set y = what[sta.top()]; sta.pop();
Set temp;
if (s[0] == 'U')
set_union(x.begin(), x.end(), y.begin(), y.end(), inserter(temp, temp.begin()));
if (s[0] == 'I')
set_intersection(x.begin(), x.end(), y.begin(), y.end(), inserter(temp, temp.begin()));
if (s[0] == 'A')
{
temp = y;
temp.insert(ID(x));
}
sta.push(ID(temp));
}
cout << (int)what[sta.top()].size() << endl;
}
cout << "***" << endl;
}
return 0;
}
【例题5-5 UVA 12096 】The SetStack Computer的更多相关文章
- UVA.12096 The SetStack Computer ( 好题 栈 STL混合应用)
UVA.12096 The SetStack Computer ( 好题 栈 STL混合应用) 题意分析 绝对的好题. 先说做完此题的收获: 1.对数据结构又有了宏观的上的认识; 2.熟悉了常用STL ...
- uva 12096 The SetStack Computer
点击打开链接uva 12096 思路: STL模拟 分析: 1 题目给定5种操作,每次输出栈顶集合的元素的个数 2 利用stack和set来模拟,set保存集合的元素.遇到push的时候直接在stac ...
- uva 12096 - The SetStack Computer(集合栈)
例题5-5 集合栈计算机(The Set Stack Computer,ACM/ICPC NWERC 2006,UVa12096) 有一个专门为了集合运算而设计的"集合栈"计算机. ...
- uva 12096 The SetStack Computer(STL set的各种库函数 交集 并集 插入迭代器)
题意: 有5种操作: PUSH:加入“{}”空集合入栈. DUP:栈顶元素再入栈. UNION:出栈两个集合,取并集入栈. INTERSECT:出栈两个集合,取交集入栈. ADD:出栈两个集合,将先出 ...
- UVa 12096 The SetStack Computer【STL】
题意:给出一个空的栈,支持集合的操作,求每次操作后,栈顶集合的元素个数 从紫书给的例子 A={{},{{}}} B={{},{{{}}}} A是栈顶元素,A是一个集合,同时作为一个集合的A,它自身里面 ...
- 12096 - The SetStack Computer UVA
Background from Wikipedia: \Set theory is a branch of mathematics created principally by the German ...
- UVa12096.The SetStack Computer
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- EOJ 1641/UVa The SetStack Computer
Background from Wikipedia: “Set theory is a branch of mathematics created principally by the German ...
- 集合栈计算机(The SetStack Computer, ACM/ICPC NWERC 2006,Uva12096)
集合栈计算机(The SetStack Computer, ACM/ICPC NWERC 2006,Uva12096) 题目描述 有一个专门为了集合运算而设计的"集合栈"计算机.该 ...
- UVA12096 - The SetStack Computer(set + map映射)
UVA12096 - The SetStack Computer(set + map映射) 题目链接 题目大意:有五个动作: push : 把一个空集合{}放到栈顶. dup : 把栈顶的集合取出来, ...
随机推荐
- php中array_merge函数
php中array_merge函数 一.array_merge简介 (PHP 4, PHP 5, PHP 7) array_merge — 合并一个或多个数组 说明¶ array array_merg ...
- java knowledge record
javax.accessibility.Accessible 给予private 或者 final 变量可以改变的机会
- artDialog提示框、对话框
/** * 警告 * @param {String}消息内容 */ artDialog.alert = function (content, callback) { return artDialog( ...
- 数据结构基础(3)---C语言实现单链表
#include<stdio.h> #include<malloc.h> #include<stdbool.h> /** **链表节点的定义 */ typedef ...
- js37---Function.prototype
//给函数的prototype新增名字为name,函数体为fn的函数 Function.prototype.method =function(name,fn){ this.prototype[name ...
- 【编程】概念的理解 —— socket
socket:A socket is something into which something is plugged or fitted (also called a receptacle). A ...
- 1.23 Python知识进阶 - 面向对象编程
一.编程方法 1.函数式编程:"函数式编程"是一种"编程范式"(programming paradigm),也就是如何编写程序的方法论.它属于"结构化 ...
- Linux shell command学习笔记(一)
Shell的种类有很多种,例如CSH,Bourne Shell,Korn Shell.在现在的大多数Linux发行版中,默认的Shell一般都是Bourne again shell(bash). &l ...
- 笔记四:onsubmit和onclick的区别
今天碰到关于表单提交的问题,我是用submit还是用onclick好呢,然后我去百度了一下两者的区别: onsubmit只能表单上使用,提交表单前会触发, onclick是按钮等控件使用, 用来触发点 ...
- HDU 1506 Largest Rectangle in a Histogram(DP)
Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...