题目大意:给你两个集合,判断两个集合的关系(不相交、相等、真子集和其他)。简单判断就可以了,不过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. 何查询SQL Server数据库没有主键的表并增加主键

    SQL Server数据库中,如果一个表没有主键,我们该如何查询呢?本文我们主要就介绍了如何查询数据库中没有主键的表名并为其增加主键的方法,希望能够对您有所帮助. 该功能的实现代码如下: declar ...

  2. 判断非法字符串的类方法,与jsp

    private String_do_judge judge; if (judge.isContain(key)) { return "feifa"; } 上面这写代码添加到进入ac ...

  3. 新手常见的python报错及解决方案

    此篇文章整理新手编写代码常见的一些错误,有些错误是粗心的错误,但对于新手而已,会折腾很长时间才搞定,所以在此总结下我遇到的一些问题.希望帮助到刚入门的朋友们.后续会不断补充. 目录 1.NameErr ...

  4. [其他]volatile 关键字

    用  volatile 关键字修饰函数 的作用是 告诉编译器该函数不会返回 , 让编译器能产生更好的代码 另外也能避免一些假警告信息,如未初始化的变量等

  5. Allegro PCB -通孔焊盘制作 及Flash制作

    通孔焊盘制作,比如插针封装 数值确定: mil单位                                                                           ...

  6. 简单三段式状态机实验1-SOS

    一直想从一段式状态机切换到三段式状态机,从书上和网上不断搜寻三段式案例及方法,感觉很简单,就想拿之前做过的实验把一段式改成三段式,可是写起来并非那么简单,很棘手,改完后也没有成功,尤其状态机里面的计数 ...

  7. zepto学习之路--源代码提取

    最近在看zepto的源代码,把一些有用的函数摘出来,看看zepto是怎么实现的,自己做的时候也可以用.说实话,zepto的实现有一些看起来还是很晦涩的,可能是自己的水平不够,看不透作者的真正的意图. ...

  8. HDU 4456(二维树状数组+坐标转换)

    题目链接:Problem - 4456 看别人叙述看的心烦,于是我自己画了一张图. 上图. 上代码 #include <iostream> #include <cstdio> ...

  9. 关于NIOS ii烧写的几种方式

    1. 方法一:.sof和.elf全部保存在FPGA内,程序加载和运行也是在FPGA内部. 把FPGA的配置文件.sof通过JTAG方式下载(其实是在线运行)进入FPGA本身,此时在NIOS II的界面 ...

  10. ural1494 Monobilliards

    Monobilliards Time limit: 1.0 secondMemory limit: 64 MB A monobilliards table set up in a gaming hou ...