uva 12096 The SetStack Computer
思路: STL模拟
分析:
1 题目给定5种操作,每次输出栈顶集合的元素的个数
2 利用stack和set来模拟,set保存集合的元素。遇到push的时候直接在stack里面push入一个空的set,遇到Dup的时候把栈顶的集合在push进stack一次,遇到union的时候把栈顶的两个集合合并,遇到Intersect的时候把栈顶的两个集合进行求交集然后push进stack,遇到Add的时候要注意如果第一个集合是空集那么我们就认为是在第二个集合里面加入2,否则就要通过map来判断当前的集合所表示的值
代码:
#include<set>
#include<map>
#include<stack>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; const int N = 20;
const int MAXN = 2010; int cnt;
stack<set<int> >stk;
map<set<int> , int>mp;
set<int>s1 , s2; void pop(){
s1 = stk.top();
stk.pop(); s2 = stk.top();
stk.pop();
}
// push
void Push(){
set<int>s;
stk.push(s);
puts("0");
}
// dup
void Dup(){
set<int>s;
s = stk.top();
stk.push(s);
printf("%d\n" , s.size());
}
// union
void Union(){
pop();
//
set<int>::iterator it;
for(it = s1.begin() ; it != s1.end() ; it++)
s2.insert(*it);
stk.push(s2);
printf("%d\n" , s2.size());
}
// Intersect
void Intersect(){
pop();
//
set<int>s3;
set<int>::iterator it;
for(it = s1.begin() ; it != s1.end() ; it++){
if(s2.find(*it) != s2.end())
s3.insert(*it);
}
stk.push(s3);
printf("%d\n" , s3.size());
}
// add
void Add(){
pop();
//
if(s1.empty())
s2.insert(0);
else{
if(!mp[s1])
mp[s1] = cnt++;
s2.insert(cnt++);
}
stk.push(s2);
printf("%d\n" , s2.size());
} int main(){
int Case , n;
char str[N];
scanf("%d" , &Case);
while(Case--){
scanf("%d" , &n);
while(!stk.empty())
stk.pop();
cnt = MAXN;
mp.clear();
while(n--){
scanf("%s" , str);
if(str[0] == 'P')
Push();
else if(str[0] == 'D')
Dup();
else if(str[0] == 'U')
Union();
else if(str[0] == 'I')
Intersect();
else
Add();
}
puts("***");
}
return 0;
}
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(集合栈)
例题5-5 集合栈计算机(The Set Stack Computer,ACM/ICPC NWERC 2006,UVa12096) 有一个专门为了集合运算而设计的"集合栈"计算机. ...
- UVa 12096 The SetStack Computer【STL】
题意:给出一个空的栈,支持集合的操作,求每次操作后,栈顶集合的元素个数 从紫书给的例子 A={{},{{}}} B={{},{{{}}}} A是栈顶元素,A是一个集合,同时作为一个集合的A,它自身里面 ...
- uva 12096 The SetStack Computer(STL set的各种库函数 交集 并集 插入迭代器)
题意: 有5种操作: PUSH:加入“{}”空集合入栈. DUP:栈顶元素再入栈. UNION:出栈两个集合,取并集入栈. INTERSECT:出栈两个集合,取交集入栈. ADD:出栈两个集合,将先出 ...
- 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 : 把栈顶的集合取出来, ...
随机推荐
- vmware10中开启Intel VT-x
记得刚接触linux的时候,是在win7下使用vmware虚拟机来安装linux,这样就可以方便的一边使用win7娱乐,一边在linux下进行学习.后来发现这种方式使得win7很卡,虚拟机也很卡,让人 ...
- Web 检测代码 web analysis 开源 open source
1. Grape Web Statistics Grape Web Statistics is a fairly simple piece of analytics software. Grape i ...
- HDU 5857 Median (推导)
Median 题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=5857 Description There is a sorted sequ ...
- esb异常20160322_1948
异常1. Failed to find entry point for component, the following resolvers tried but failed: [ 2016-03-2 ...
- Chocolatey的安装与使用
@(编程) 前言 在 Linux 下,大家喜欢用 apt-get 来安装应用程序,如今在 windows 下,大家可以使用 Chocolatey 来快速下载搭建一个开发环境. Chocolatey 的 ...
- hdu 2899 Strange fuction
http://acm.hdu.edu.cn/showproblem.php?pid=2899 Strange fuction Time Limit: 2000/1000 MS (Java/Others ...
- 关于 三星 I9100 (水货)
前天陪好友去买水货9100,总结了一点经验,觉得挺有用的,今天整理一下写出来...有 需要的可以看看..原创整理.. 一,当然是检查外观(检查USB接口有没有磨损,检查摄像头是否有灰尘,检查屏幕是不是 ...
- McAfee Host Intrusion Prevention
McAfee Host Intrusion Prevention是一款集防火墙功能和HIPS于一身的主动防御和防火墙软件,将其与 McAfee VirusScan Enterprise 8.5/8.7 ...
- [iOS微博项目 - 2.6] - 获取微博数据
github: https://github.com/hellovoidworld/HVWWeibo A.新浪获取微博API 1.读取微博API 2.“statuses/home_time ...
- JS初识(着重讲解Date函数)
查看类型:typeof() 转换为int类型:parseInt() isNaN() 函数用于检查其参数是否是非数字值. NaN,是Not a Number的缩写.一种计算机用语.NaN 用于处理计算中 ...