【PAT】1063. Set Similarity (25)【Use of set container】

Topic description

Given two sets of integers, the similarity of the sets is defined to be Nc/Nt*100%, 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.

Translation: You are given two sets of integers, the similarity between them is defined as Nc/Nt*100%, where NC is a specific number common between the two sets, and Nt is the total number of different numbers in the two sets. Your task is to compute the similarity of a given pair of sets.

INPUT FORMAT

Each input file contains one test case. Each case first gives a positive integer N (<=50) which is the total number of sets. Then N lines follow, each gives a set with a positive M (<=104) and followed by M integers in the range [0, 109]. After the input of sets, a positive integer K (<=2000) 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.

Translation: Each input file contains a set of test data. For each set of input data, the first line includes a positive integer N (<=50) representing the total number of sets. Next N lines, each giving a set, first M (<=10^4), followed by M integers in the range {0-10^9}. After the input of the set is completed, it is a positive integer K (<=2000), followed by K lines of query. Each query contains a pair of collection numbers (collections are numbered 1-N). All numbers on a line are separated by spaces.

OUTPUT FORMAT

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

Translation: For each set of queries, output a row of the similarity between sets, expressed as a percentage with one 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%


Problem solving ideas

The title clearly points out that set, of course, is to use the set container. The number of intersections in two sets needs to traverse one set, and then use the count() function to determine whether it appears in the other set; the number of unions is the sum of the size() of the two sets minus the number of intersections. Remember to convert to percentage output when outputting.

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<set> 
#include<algorithm>
#define INF 99999999
using namespace std;
set<int> st[60];
set<int>::iterator it;
int N,K;
int main(){
    scanf("%d",&N);
    int M;
    for(int i=1;i<=N;i++){
        scanf("%d",&M);
        int a;
        for(int j=0;j<M;j++){
            scanf("%d",&a);
            st[i].insert(a);
        }
    }
    scanf("%d",&K);
    int p,q;
    for(int i=0;i<K;i++){
        int sum,common=0;
        scanf("%d%d",&p,&q);
        for(it=st[p].begin();it!=st[p].end();it++){
            if(st[q].count(*it))common++;
        }
        sum=st[p].size()+st[q].size()-common;
        double ans=common*1.0/sum;
        ans*=100;
        printf("%.1lf%%\n",ans);
    }
    return 0;
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324819786&siteId=291194637