Given two sets of integers, their similarity is defined as: Nc /Nt ×100%. Where Nc
is the number of unequal integers shared by the two sets, and Nt is the number of unequal integers shared by the two sets. Your task is to calculate the similarity of any pair of given sets.
Input format: The
first line of input gives a positive integer N (≤50), which is the number of sets. Next N rows, each row corresponds to a collection. Each set is first given a positive integer M (≤104), which is the number of elements in the set; then followed by M integers in the interval [0,109].
The next line gives a positive integer K (≤2000), and then K lines, each line corresponds to the number of a pair of sets that need to calculate the similarity (the sets are numbered from 1 to N). The numbers are separated by spaces.
Output format:
For each pair of sets that need to be calculated, output their similarity in one line, which is a percentage number with 2 decimal places.
Input sample:
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.00%
33.33%
Code:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n,m,k,x,a,b;
set<int> g[60];
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>m;
for(int j=0;j<m;j++)
{
cin>>x;
g[i].insert(x);
}
}
cin>>k;
for(int i=0;i<k;i++)
{
int cnt=0;
cin>>a>>b;
for(auto it=g[a].begin();it!=g[a].end();it++)
if(g[b].find(*it)!=g[b].end()) cnt++; //Nc
double ans=cnt*100.0/(g[a].size()+g[b].size()-cnt);
printf("%.2f%%\n",ans);
}
return 0;
}