OJ 1350 Problem C STL——Jerry的问题

Description

最近Jerry正在刻苦的学习STL中的set的功能函数,他发现set可以用现有的函数实现并、交、差、对称差等功能,但是他没有找到怎么来比较两个集合是否相等的功能函数,所以他想自己用其他的功能函数来实现能判断两个集合是否相等的功能函数。聪明的Jerry不一会就想到了解决办法,现在他想拿这道题来考考你,看你有没有他聪明。

Input

输入有多组,每组数据有两行,每一行都代表一个集合,每一行有若干个正整数(0<d<=2147483647),并且每行的最后一个数字都是0,代表该行数据的结束,且末尾的0不计入集合中。最后以EOF结束输入。

Output

对于每组数据输出都要输入一个结果,如果两个集合相等便输出“YES”,否则输出“NO”,每个结果占一行

Sample Input

1 2 3 4 0
1 2 3 4 0
1 2 2 2 2 2 0
1 2 0
1 2 3 4 0
1 3 3 4 0

Sample Output

YES
YES
NO

HINT

对称差运算:得到的结果是第一个集合与第二个集合的差集并上第二个与第一个的差集

Accepted Code

#include <iostream>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <list>
#include <algorithm>
#include <string>
#include <set>
#include <cstdio>
using namespace std;
int main() {
    int num;
    while (cin >> num) {
        set<int> A ,B;
        set<int> s1, s2, s3;
        if (num) {  //注意细节
            A.insert(num);
            while (cin >> num && num)
                A.insert(num);
        }
        while (cin >> num && num)
            B.insert(num);
        set_difference(A.begin(), A.end(), B.begin(), B.end(), inserter(s1, s1.begin()));
        set_difference(B.begin(), B.end(), A.begin(), A.end(), inserter(s2, s2.begin()));
        set_union(s1.begin(), s1.end(), s2.begin(), s2.end(), inserter(s3, s3.begin()));
        if(s3.empty()) cout << "YES" << endl;
        else cout << "NO" << endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值