【例题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 : 把栈顶的集合取出来, ...
随机推荐
- android 图片特效处理之锐化效果
这篇将讲到图片特效处理的锐化效果.跟前面一样是对像素点进行处理,算法是通用的. 算法原理: 一.简单算法:分别获取当前像素点和八个周围像素点的RGB值,先求出当前像素点的RGB值与八个像素点RGB值的 ...
- js关于循环的理解
学习任何语言都离不开循环,js也是一样,看了网上的资料,整理一份关于js循环的理解. 1.最基础循环,js和其他高级语言一样使用for.while循环 (function() { for(var i= ...
- Android框架之路——OkGo的使用
一.简介 该库是封装了okhttp的标准RESTful风格的网络框架,可以与RxJava完美结合,比Retrofit更简单易用.支持大文件上传下载,上传进度回调,下载进度回调,表单上传(多文件和多参数 ...
- Oracel 格式化日期 to_char()
select empno,ename,job,mgr,to_char(HIREDATE,'yyyy-mm-dd') as 入职日期,sal,comm,deptno from emp order by ...
- [Angular] Make a chatbot with DialogFlow
Register a account on https://console.dialogflow.com/api-client/ "Creat a intent" -- you c ...
- Centos 6.8 安装 Protocol Buffers , v3.2.0有 BUG ,安装 3.1.0
Centos 6.8 安装 Protocol Buffers , v3.2.0有 BUG ,安装 3.1.0 切换到用户目录 cd ~ 安装 python2.7,须加入zlib wget http ...
- Ubuntu 14 中给 APACHE2安装 SSL 模块 Enable SSL site on Ubuntu 14 LTS, Apache 2.4.7:
Ubuntu 14 中给 APACHE2安装 SSL 模块 Enable SSL site on Ubuntu 14 LTS, Apache 2.4.7: 参考 http://blog.csdn.ne ...
- 矩阵乘法java代码实现
矩阵只有当左边矩阵的列数等于右边矩阵的行数时,它们才可以相乘, 乘积矩阵的行数等于左边矩阵的行数,乘积矩阵的列数等于右边矩阵的列数 即A矩阵m*n,B矩阵n*p,C矩阵m*p: package exa ...
- Python-根据成绩分析是否继续深造
案例:该数据集的是一个关于每个学生成绩的数据集,接下来我们对该数据集进行分析,判断学生是否适合继续深造 数据集特征展示 GRE 成绩 (290 to 340) TOEFL 成绩(92 to 120) ...
- jQuery UI:邮箱自动补全函数
$('#email').autocomplete({ delay:0, autoFocus:true, source:function(request,response){ var hosts = [ ...