The solution to the 002 Set intersection algorithm of EI judge

Source: Internet
Author: User

Set intersection

Time limit = 5 second (s)

Memory limit = 33000 Kb

Your task is to find the intersection of two sets of positive integers.

Input The input consists of two sets of positive integers a ={a1, a2, ..., a n} and b={b1, b2, ..., bk} Represented as two whitespace-separated lists of numbers. Each list ends With-1, which serves as an end-of-set marker. Repetitions of elements are possible in the input, but not in the output. May assume that 0 < ai, bi < ten6 and that the Sets A and B are of size less than 1000.

Output is list of numbers or word ' empty ' if intersection is empty.

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

Note the place:

1 prevent repetition--use set here, the second time to enumerate the group, remember to erase set heavy data

2 output format This OJ is also very strict, not one more space

 #include <iostream> #include <string> #include <vector> #include <algorithm> #includ  
      
e <set> using namespace std;  
      
int a[1000] = {0};  
    void Setintersection () {set<int> si;  
    int a = 0;  
    while (Cin>>a &&-1!= a) {Si.insert (a);  
    int j = 0;  
            while (Cin>>a && 1!= a) {if (Si.count (a)) {a[j++] = A;  
        Si.erase (a);  
    } if (!j) cout<< "Empty";  
        else {j--;  
        for (int i = 0; i < J; i++) {cout<<a[i]<< '; cout<<a[j];//original to prevent multiple output of one '}} 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.