题目大意:给你两个集合,判断两个集合的关系(不相交、相等、真子集和其他)。简单判断就可以了,不过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的更多相关文章

  1. UVa 496 Simply Subsets (STL&set_intersection)

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=sh ...

  2. 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 ...

  3. UVa 10048: Audiophobia

    这道题要求我们求出图中的给定的两个节点(一个起点一个终点,但这是无向图)之间所有“路径中最大权值”的最小值,这无疑是动态规划. 我开始时想到根据起点和终点用动态规划直接求结果,但最终由于题中S过大,会 ...

  4. uva 10192 Vacation(最长公共子)

    uva 10192 Vacation The Problem You are planning to take some rest and to go out on vacation, but you ...

  5. UVa 10012 - How Big Is It? 堆球问题 全排列+坐标模拟 数据

    题意:给出几个圆的半径,贴着底下排放在一个长方形里面,求出如何摆放能使长方形底下长度最短. 由于球的个数不会超过8, 所以用全排列一个一个计算底下的长度,然后记录最短就行了. 全排列用next_per ...

  6. 【Lintcode】018.Subsets II

    题目: Given a list of numbers that may has duplicate numbers, return all possible subsets Notice Each ...

  7. UVA 11488 Hyper Prefix Sets (Trie)

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  8. uva live 6827 Galaxy collision

    就是给出非常多点,要求分成两个集合,在同一个集合里的点要求随意两个之间的距离都大于5. 求一个集合.它的点数目是全部可能答案中最少的. 直接从随意一个点爆搜,把它范围内的点都丢到跟它不一样的集合里.不 ...

  9. Subsets II

    Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...

随机推荐

  1. 提升html5的性能体验系列之二列表流畅滑动

    App的顶部一般有titlebar,下面是list.常见的一个需求是要在list滚动时,titlebar不动.这个简单的需求,实现起来其实并不简单. 在普通web上的做法是使用div的滚动条,把lis ...

  2. 剑指offer 整数中1 出现的次数

    给定一个十进制正整数N,写下从1开始,到N的所有整数,然后数一下其中出现的所有"1"的个数. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 1 ...

  3. mongodb从来没有说它写成功了。

    它只是说,yes,sir. http://www.infoq.com/cn/news/2014/04/bitcoin-banking-mongodb 开头的表述是错误的,官方说法它有4种类型的writ ...

  4. pop动画使用示例

    // 弹簧动画 POPSpringAnimation *anim = [POPSpringAnimation animationWithPropertyNamed:kPOPViewFrame]; an ...

  5. VS下载地址

    http://www.iplaysoft.com/vs2015.html   Microsoft Visual Studio(简称VS)是美国微软公司的开发工具包系列产品.Visual Studio ...

  6. git分支综述

    对git不是很熟悉,有个问题要弄清楚一下. 假如远程仓库有 dev 和 master 两个分支,master 作为一个稳定版分支,可用于直接发布产品,日常的开发则 push 到 dev 分支,那我本地 ...

  7. bi api 软件

    https://www.interactivebrokers.com.hk/cn/index.php?f=5234&ns=T

  8. create schema 与create database的区别

    概论第四版中增加了create schema (第三版好像没有这个内容),但是却没有提到create  database.相反目前在大多数DBMS中(如SQL Server2000.Mysql等)都有 ...

  9. Lua学习系列(三)

    Ubuntu14.04 上源码编译安装lua5.3 原文:http://blog.csdn.net/abclixu123/article/details/46676991

  10. Struts2 JSP中将list,set ,Map传递到Action然后遍历(三十五) - 雲淡風輕 - ITeye技术网站

    1.使用Strut2的的集合对象:在jsp初始化action中的list然后提交到action2.使用Struts标签,实现多个用户同时注册(注意属性配置文件)3.pojo package com.s ...