# PAT-A 真题 – 1063 Set Similarity

Given two sets of integers, the similarity of the sets is defined to be , where  is the number of distinct common numbers shared by the two sets, and  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  () which is the total number of sets. Then  lines follow, each gives a set with a positive  () and followed by  integers in the range []. After the input of sets, a positive integer  () is given, followed by  lines of queries. Each query gives a pair of set numbers (the sets are numbered from 1 to ). 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%

Nc的计算用set::find()函数即可，Nt的计算可以用两个集合元素总数量-公共元素数量

#include <bits/stdc++.h>
using namespace std;

int main(){
int cnt;
scanf("%d", &cnt);
vector<set<int> > V(cnt + 1);
for(int i = 1; i <= cnt; i++){
int icnt;
scanf("%d", &icnt);
while(icnt--){
int t;
scanf("%d", &t);
V[i].insert(t);
}
}
scanf("%d", &cnt);
while(cnt--){
int a, b;
scanf("%d %d", &a, &b);
int Nc = 0, Nt = V[b].size();
for(auto it : V[a]){
if(V[b].find(it) != V[b].end()) Nc++;
else Nt++;
}
printf("%.01f%%\n", 100.0 * Nc / Nt);
}
return 0;
}