1063 Set Similarity (25point (s)) require two brush * set of ideas, we must pay attention

The basic idea:

Their method of use is to set the direct recording, too awkward to add two traverse directly together, but it is worth learning sample code;

 

key point:

Set union intersection thought, can have a direct record of the number of SET, the second traverse directly;

 

If using the first set.find, find the same element number recorded directly intersection +1; find different elements, the intersection of +1 elements;

 

double charge(int a, int b) {
    double jn=0, bn=0;
    BN = VEC [A] .size ();
     for (Auto IT VEC = [B] .begin (); IT VEC = [B] .end ();! IT ++ ) {
         IF (VEC [A] .find (* ! IT) = VEC [A] .end ()) {
             // if find the same element; 
            JN ++ ;
        }
        else {
            the bn ++ ;
        }
    }
    return jn/bn;
}

 

 

Overall Code:

#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<vector> 
#include<string>
#include<math.h>
#include<algorithm>
#include<cstring>
#include<map>
#include<queue>
#include<set>
using namespace std;

int n,m;
vector<set<int>>vec;

double charge(int a, int b) {
    double jn=0, bn=0;
    BN = VEC [A] .size ();
     for (Auto IT VEC = [B] .begin (); IT VEC = [B] .end ();! IT ++ ) {
         IF (VEC [A] .find (* ! IT) = VEC [A] .end ()) {
             // if find the same element; 
            JN ++ ;
        }
        else {
            the bn ++ ;
        }
    }
    return jn/bn;
}

int main () {
    scanf("%d", &n);
    int a,b;
    for (int i = 0; i < n; i++) {
        set<int>s;
        scanf("%d", &a);
        for (int j = 0; j < a; j++) {
            scanf("%d", &b);
            s.insert(b);
        }
        vec.push_back(s);
    }
    scanf("%d", &m);
    for (int i = 0; i < m; i++) {
        scanf("%d %d", &a, &b);
        double re = charge(a - 1, b - 1)*100;
        //cout << re << endl;
        printf("%.1lf%%\n", re);
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/songlinxuan/p/12340726.html