UVa 496 - Simply Subsets
题目大意:给你两个集合,判断两个集合的关系(不相交、相等、真子集和其他)。简单判断就可以了,不过STL的set没有交集、并集等操作有点让人觉得不方便...
#include <cstdio>
#include <iostream>
#include <set>
using namespace std; set<int> intersection(const set<int> &a, const set<int> &b)
{
set<int>::iterator itA = a.begin(), itB = b.begin();
set<int> c;
while (itA != a.end() && itB != b.end())
{
if (*itA == *itB)
{
c.insert(*itA);
itA++;
itB++;
}
else if (*itA < *itB) itA++;
else if (*itA > *itB) itB++;
}
return c;
} int main()
{
#ifdef LOCAL
freopen("in", "r", stdin);
#endif
int x;
while (scanf("%d", &x) != EOF)
{
set<int> A, B, C;
A.insert(x);
C.insert(x);
while (getchar() != '\n')
{
scanf("%d", &x);
A.insert(x);
C.insert(x);
}
scanf("%d", &x);
B.insert(x);
C.insert(x);
while (getchar() != '\n')
{
scanf("%d", &x);
B.insert(x);
C.insert(x);
}
if (A.size() + B.size() == C.size()) printf("A and B are disjoint\n");
else if (A == B) printf("A equals B\n");
else
{
set<int> s = intersection(A, B);
if (A == s) printf("A is a proper subset of B\n");
else if (B == s) printf("B is a proper subset of A\n");
else printf("I'm confused!\n");
}
}
return ;
}
@2013-11-10 11:50:07
前两天看C++ Primer的时候,看到标准库里有set_union(), set_intersection(), set_difference() 和 set_symmetirc_difference()函数,就重写了一下,提交后和上面那个代码时间一样,不过省了不少功夫哈,对c++还是不熟悉啊,还要多学习。
#include <cstdio>
#include <iostream>
#include <set>
#include <algorithm>
using namespace std; int main()
{
#ifdef LOCAL
freopen("in", "r", stdin);
#endif
int x;
while (scanf("%d", &x) != EOF)
{
set<int> A, B, C;
A.insert(x);
C.insert(x);
while (getchar() != '\n')
{
scanf("%d", &x);
A.insert(x);
C.insert(x);
}
scanf("%d", &x);
B.insert(x);
C.insert(x);
while (getchar() != '\n')
{
scanf("%d", &x);
B.insert(x);
C.insert(x);
}
if (A.size() + B.size() == C.size()) printf("A and B are disjoint\n");
else if (A == B) printf("A equals B\n");
else
{
set<int> s;
set_intersection(A.begin(), A.end(), B.begin(), B.end(), inserter(s, s.begin()));
if (A == s) printf("A is a proper subset of B\n");
else if (B == s) printf("B is a proper subset of A\n");
else printf("I'm confused!\n");
}
}
return ;
}
UVa 496 - Simply Subsets的更多相关文章
- UVa 496 Simply Subsets (STL&set_intersection)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=sh ...
- uva 387 A Puzzling Problem (回溯)
A Puzzling Problem The goal of this problem is to write a program which will take from 1 to 5 puzz ...
- UVa 10048: Audiophobia
这道题要求我们求出图中的给定的两个节点(一个起点一个终点,但这是无向图)之间所有“路径中最大权值”的最小值,这无疑是动态规划. 我开始时想到根据起点和终点用动态规划直接求结果,但最终由于题中S过大,会 ...
- uva 10192 Vacation(最长公共子)
uva 10192 Vacation The Problem You are planning to take some rest and to go out on vacation, but you ...
- UVa 10012 - How Big Is It? 堆球问题 全排列+坐标模拟 数据
题意:给出几个圆的半径,贴着底下排放在一个长方形里面,求出如何摆放能使长方形底下长度最短. 由于球的个数不会超过8, 所以用全排列一个一个计算底下的长度,然后记录最短就行了. 全排列用next_per ...
- 【Lintcode】018.Subsets II
题目: Given a list of numbers that may has duplicate numbers, return all possible subsets Notice Each ...
- UVA 11488 Hyper Prefix Sets (Trie)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- uva live 6827 Galaxy collision
就是给出非常多点,要求分成两个集合,在同一个集合里的点要求随意两个之间的距离都大于5. 求一个集合.它的点数目是全部可能答案中最少的. 直接从随意一个点爆搜,把它范围内的点都丢到跟它不一样的集合里.不 ...
- Subsets II
Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...
随机推荐
- ZOJ Problem - 2588 Burning Bridges tarjan算法求割边
题意:求无向图的割边. 思路:tarjan算法求割边,访问到一个点,如果这个点的low值比它的dfn值大,它就是割边,直接ans++(之所以可以直接ans++,是因为他与割点不同,每条边只访问了一遍) ...
- 2015十大顶级开源ERP系统点评
如今,企业资源规划(ERP)和客户关系管理(CRM)系统的必要性已经被各种组织和企业所认可:ERP和CRM能够直接为企业的业务效率和利润做出贡献. 但是随着今天企业商业形态的日趋多样化,互联网新经济的 ...
- ArrayList遍历的同时删除--- 删除还是用迭代器的比较好,其它的都会有问题.
http://javag.iteye.com/blog/403097 ArrayList遍历的同时删除-- 删除还是用迭代器的比较好,其它的都会有问题. 博客分类: 随笔 Javathread ...
- Css span div
SPAN元素和DIV元素有什么区别 解决思路: 最明显的区别是:DIV是块元素,SPAN是内嵌元素.块元素相当于内嵌元素在前后各加一个<br>换行.其实,块元素和行内元素也不是一成不变的, ...
- 关于Jsp页面在ww:iterator 标签里面判断的写法是可以直接写数组里面的变量的
因为上面已经遍历了,所以可以直接写变量名
- 2的幂次方(power)
2的幂次方(power) 题目描述 任何一个正整数都可以用2的幂次方表示.例如:137=27+23+20同时约定方次用括号来表示,即ab 可表示为a(b). 由此可知,137可表示为:2(7)+2(3 ...
- Android学习之Service(1)--->Started方式
界面退出后进程程序还在运行,不会被杀死,如音乐播发器.后台下载等 注:本文只讨论Started方式 main.xml代码分析 <?xml version="1.0" enco ...
- word2016怎么从第三页开始设置页码
在Word中,默认为一节,每节的页码是连续的,若想在一个Word文档中,前2页不设页码,从第3页开始设置页码,必须插入分节符,使文档分成两节,这样就可以单独设置页码.操作步骤:1.将光标放在第4页的首 ...
- 浏览器UA汇总
UA -- uesr-agent -- 用户代理,是服务器判断请求的种类,比如:使用PC和手机访问一个网站,呈现的画面是不一样的. 原理就是设备的用户代理不同 1 主要浏览器 safari 5.1 ...
- html精确定位
1.offsetwidth.offsetHeight是指包含border的元素宽高. 2.clientWidth.clientHeight是指不包含border的元素宽高. 3.scrollWidth ...