题目大意:给你两个集合,判断两个集合的关系(不相交、相等、真子集和其他)。简单判断就可以了,不过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. HDU-1232--畅通工程(最小生成树)

    Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府"畅通工程"的目标是使全省任何两个城镇间都可以实现交通 ...

  2. RedHat虚拟机:Vmware Tools的安装

    如果我们仔细看的话,                  就会发现在VMware软件界面的左下角处显示着                  “you don't have VMware Tools in ...

  3. 在TTF字体中提取想要的文字

    工具地址:https://yunpan.cn/cSLhX5jXnxFZg  访问密码 8000 1. 确保你的电脑已经安装了Java环境(能运行Java命令),这是必须的. 2.复制要提取的源字体(j ...

  4. android 设计模式学习

    1:单例模式 //对于创建开销较大的类可使用此方法,保证全局一个实例,在程序运行过程中该类不会因新建额外对象产生开销.示例代码如下:public class Singleton { private s ...

  5. [转]Linux下CodeBlocks的交叉编译

    原文链接:http://blog.sina.com.cn/s/blog_602f87700100kujh.html Sam一直是Makefile流,这些天需要移植一些游戏引擎模块.这些模块在其它嵌入式 ...

  6. asp 自我定时删除

    <% if now()>"2008-9-15" thenset myfso=server.CreateObject("scripting.filesystem ...

  7. 第15章 I/O(输入/输出)

    在变量.数组和对象中存储的数据是暂时存在的,程序结束后它们就会丢失.为了能够永久地保存创建的数据,需要将其保存在磁盘文件中,这样就可以在其它程序中使用它们.Java的I/O技术可以将数据保存到文本文件 ...

  8. JQuery中一个简单的表单验证的实例

    html代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://w ...

  9. hudson--ant编写记录

    最近配置Hudson---持续集成工具,重点是ant的编写. 环境:Ubuntu 虚拟机 hudson系统设置里面jdk ant路径也是Ubuntu里文件路径如:/home/test/java/ant ...

  10. CodeForces 158B Taxi(贪心)

    贪心,注意优先级,4单独,3与1先匹配,2与2匹配(注意判断2有没有剩下),然后2与两个1匹配,最后4个1匹配就可以了. #include<iostream> #include<cs ...