1063 Set Similarity

Given two sets of integers, the similarity of the sets is defined to be /, where Nc​​ is the number of distinct common numbers shared by the two sets, and Nt​​ is the total number of distinct numbers in the two sets. Your job is to calculate the similarity of any given pair of sets.

Input Specification:

Each input file contains one test case. Each case first gives a positive integer N (≤) which is the total number of sets. Then N lines follow, each gives a set with a positive M (≤) and followed by M integers in the range [0]. After the input of sets, a positive integer K (≤) is given, followed by K lines of queries. Each query gives a pair of set numbers (the sets are numbered from 1 to N). All the numbers in a line are separated by a space.

Output Specification:

For each query, print in one line the similarity of the sets, in the percentage form accurate up to 1 decimal place.

Sample Input:

3
3 99 87 101
4 87 101 5 87
7 99 101 18 5 135 18 99
2
1 2
1 3
 

Sample Output:

50.0%
33.3%
 
#include<stdio.h>
#include<set>
using namespace std;
struct Setcoop
{
    set<int> aset;
}setcoop[100];

int main()
{
    Setcoop setcoop2[2002];
    int n;
    scanf("%d", &n);
    int setnum;
    for (int i =1; i <=n; i++)
    {
        scanf("%d", &setnum);
        for (int j = 0; j < setnum; j++)
        {
            int inset;
            scanf("%d", &inset);
            setcoop[i].aset.insert(inset);
        }
    }
    int putoutnum;
    scanf("%d", &putoutnum);
    double baifenbi[2002];

 
    for (int x = 0; x < putoutnum; x++)
    {
        double cha=0;
        int set1;
        int set2;
        scanf("%d", &set1);
        scanf("%d", &set2);
        double qian = setcoop[set1].aset.size() + setcoop[set2].aset.size();
        set<int>::iterator it=setcoop[set1].aset.begin();
        for(;it!=setcoop[set1].aset.end();it++)
        {                                                                        //注意未查找到时指针移至末尾 
                if(setcoop[set2].aset.find(*it)!=setcoop[set2].aset.end()) cha++;    //find为nlogn  如果直接查为n2,超时 
                                                                                            //vs那种为insert,复杂度n,乘外层n也有n2了 
        }
        baifenbi[x] = cha / (qian-cha) * 100;
    }
    for (int n = 0; n < putoutnum; n++)
    {
        if (n!=putoutnum-1)
        printf("%0.1lf%%\n", baifenbi[n]);
        else
        printf("%0.1lf%%", baifenbi[n]);
    }
    
}

 

 
posted @ 2020-03-21 21:22  zzzlight  阅读(190)  评论(0编辑  收藏  举报