OJ: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
#include <iostream>
#include <set>
#include <map>

using namespace std;
int main()
{
    int n;
    while(cin >> n)
    {
        set<int>A;
        set<int>B;
        map<int, int>AB;
        map<int, int>BA;
        int m;
        if(n!=0)
        {
            A.insert(n);
            AB[n] = 1;
            while(cin >> m && m!=0)
            {
                A.insert(m);
                AB[m] = 1;
            }
        }
        int t = 0;
        while(cin >> m && m!=0)
        {
            B.insert(m);
            BA[m] = 1;
        }
        set<int>::iterator it = B.begin();
        while(it!=B.end())
        {
            if(AB[*it]!=0)
            t++;
            it++;
        }
        if(t == B.size())
        {
            t = 0;
            it = A.begin();
            while(it!=A.end())
            {
                if(BA[*it]!=0)
                t++;
                it++;
            }
            if(t == A.size())
                cout << "YES" << endl;
            else
                cout << "NO" <<endl;
        }
        else
            cout << "NO" <<endl;
    }
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章