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 ...
随机推荐
- 关于html5的几个新标签在IE9之前不支持的解决办法
IE8及之前的浏览器不支持用CSS的方法来使用这些尚未支持的结构元素,为了在Internet Explorer浏览器中也能正常使用这些结构元素,需要使用JavaScript脚本,如下:<scri ...
- JS定时器的使用--数码时钟
<title>无标题文档</title> <script> function toDou(n){ if(n<10){ return '0'+n; }else{ ...
- ARP及RARP简介
1.概念 ARP(Address Resolution Protocol) 地址解析协议 RARP(Reverse Address Resolution Protocol) 反向地址解析协议 2.AR ...
- PowerDesigner 生成数据库字典(有图有真相,绝对自创非转载)
最近用pd做模型,生成数据字典时在网上找了很多,但是看的都很晕,说的不明白. 经过自己研究终于找到一个简单的方式,当然这只是简单的,大家举一反三去吧.辛苦弄的,求点赞!!! 先看效果图: 现在说一下步 ...
- mysql数据库主从备份
近期实验室总是不给通知的就停电,导致我们在不停的恢复服务.在某一个断电的过程中,发现我们的项目管理工具redmine的硬盘挂掉了..因为是部署在虚拟机上的,也没做冗余,数据就丢了..于是反思,我们的m ...
- 最短路径算法专题2----Dijkstra
这个算法适用于求单源最短路径,从一点出发,到其余个点的最短路径. 算法要点: 1.用二维数组存放点到点的距离-----不能相互到达的点用MAX代替距离 2.用dis数组存放源点到任意其他一点的距离-- ...
- [转载] ASP.NET MVC4使用百度UEDITOR编辑器
前言 配置.net mvc4项目使用ueditor编辑器,在配置过程中遇见了好几个问题,以此来记录解决办法.编辑器可以到http://ueditor.baidu.com/website/downloa ...
- 使用Pushlet将消息从服务器端推送到客户端
使用Pushlet来实现服务器端向客户端推送信息 1. 实现方式: 有两种实现方式: 1. 通过配置文件来实现定时的从服务器端向客户端推送信息 2. 通过API主动 ...
- JS动态生成<style>
var nod = document.createElement(“style”), str = “body{background:#000;color:#fff} a{color:#fff;text ...
- iOS开发——GCDAsyncSocket
新进的这家公司搞智能家居,就随便整理一下其相关技术吧!首先,从GCDAsyncSocket的使用问题着手. 正如名称一样GCDAsyncSocket开源类库是以苹果的GCD多任务处理机制完成的一个异步 ...