Fork me on GitHub

PAT(甲级)渡劫(二十一)-Set Similarity(25)


PAT(甲级)渡劫(二十一)-Set Similarity(25)

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <cstdio>
#include <set>
#include <vector>

using namespace std;

vector<set<int> > v; // 集合数组

double Similarity(set<int> &a,set<int> &b){
set<int>::iterator x = a.begin();
set<int>::iterator y = b.begin();
int samecnt = 0;
while(x != a.end() && y != b.end()){
if(*x == *y){
samecnt++;
++x;
++y;
}else if(*x < *y) ++x;
else ++y;
}
double ret = 1.0*samecnt/(a.size()+b.size()-samecnt);
return ret;
}

int main(){
freopen("in.txt","r",stdin);
int n,q;
scanf("%d",&n);
v.resize(n+1);
for(int i = 1 ; i <= n ; i++){
int m;
scanf("%d",&m);
while(m--){
int x;
scanf("%d",&x);
v[i].insert(x);
}
}
scanf("%d",&q);
while(q--){
int x,y;
scanf("%d%d",&x,&y);
double ret = Similarity(v[x],v[y])*100;
printf("%.1lf%%\n",ret);
}

return 0;
}

运行结果:

坚持原创技术分享,您的支持将鼓励我继续创作
-------------本文结束感谢您的阅读-------------
0%