1063 Set Similarity (25 分)

it2022-05-05  203

1063 Set Similarity (25 分)  

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%   一个贪心过程,要求每次都是不一样的。   1 #include <bits/stdc++.h> 2 3 using namespace std; 4 int n,m,x,y,k; 5 vector<int> v[60]; 6 7 int main(){ 8 scanf("%d", &n); 9 for(int i = 1; i <= n; i++){ 10 scanf("%d", &k); 11 for(int j = 0 ; j < k; j++){ 12 scanf("%d",&m); 13 v[i].push_back(m); 14 } 15 sort(v[i].begin(), v[i].end()); 16 } 17 scanf("%d",&m); 18 while(m--){ 19 scanf("%d%d", &x,&y); 20 int same = 0, all = 0; 21 int i = 0, j = 0; 22 while(i < v[x].size() && j < v[y].size()){ 23 if(v[x][i] < v[y][j]){ 24 all++; 25 i++; 26 }else if(v[x][i] > v[y][j]){ 27 all++; 28 j++; 29 }else{ 30 all++; 31 same++; 32 i++;j++; 33 } 34 while(i &&i < v[x].size()&& v[x][i] == v[x][i-1]){ 35 i++; 36 } 37 while(j &&j < v[y].size()&& v[y][j] == v[y][j-1]){ 38 j++; 39 } 40 } 41 while(i < v[x].size()){ 42 all++; 43 i++; 44 while(i &&i < v[x].size()&& v[x][i] == v[x][i-1]){ 45 i++; 46 } 47 } 48 while(j < v[y].size()){ 49 all++; 50 j++; 51 while(j &&j < v[y].size()&& v[y][j] == v[y][j-1]){ 52 j++; 53 } 54 } 55 // cout << same << " " << all <<endl; 56 double ans = (same*1.0)/(all*1.0)*100.0; 57 printf("%0.1lf", ans); 58 cout<<"%"<<endl; 59 } 60 61 return 0; 62 }

 

     

转载于:https://www.cnblogs.com/zllwxm123/p/11193434.html


最新回复(0)