pat1063. Set Similarity (25)

1063. Set Similarity (25)

时间限制
300 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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.

Input Specification:

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.

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<cstdio>
 2 #include<stack>
 3 #include<cstring>
 4 #include<iostream>
 5 #include<stack>
 6 #include<set>
 7 #include<map>
 8 using namespace std;
 9 map<int,set<int> > ha;
10 int main(){
11     //freopen("D:\\INPUT.txt","r",stdin);
12     int n,m;
13     scanf("%d",&n);
14     int i,j,num;
15     for(i=1;i<=n;i++){
16         scanf("%d",&m);
17         for(j=0;j<m;j++){
18             scanf("%d",&num);
19             ha[i].insert(num);
20         }
21     }
22     /*for(i=1;i<=n;i++){
23         cout<<"i:  "<<i<<endl;
24         set<int>::iterator it;
25         for(it=ha[i].begin();it!=ha[i].end();it++){
26             cout<<*it<<endl;
27         }
28     }*/
29     scanf("%d",&m);
30     int a,b;
31     set<int>::iterator it;
32     for(i=0;i<m;i++){
33         scanf("%d %d",&a,&b);
34         int sum=0;
35         for(it=ha[a].begin();it!=ha[a].end();it++){
36             if(ha[b].count(*it)){
37                 sum++;
38             }
39         }
40         //cout<<sum<<" "<<ha[b].size()+ha[a].size()<<endl;
41         printf("%.1lf%\n",sum*1.0*100/(ha[b].size()+ha[a].size()-sum));
42     }
43     return 0;
44 }

 

posted @ 2015-09-02 21:54  Deribs4  阅读(598)  评论(0编辑  收藏  举报