团体程序设计天梯赛-练习集
L2-005. 集合相似度
https://www.patest.cn/contests/gplt/L2-005
先用数组模拟结果超时,用set就好了。
#include<cstdio>
#include<set>
#include<cstdlib>
using namespace std;
const int maxn = 55;
set<int> s[maxn];
int main() {
int n;
while (scanf("%d", &n) != EOF) {
for (int i = 1; i <= n; i++) {
if (!s[i].empty())s[i].clear();
int m;
scanf("%d", &m);
for (int j = 0; j < m; j++) {
int t;
scanf("%d", &t);
s[i].insert(t);
}
}
int ca;
scanf("%d", &ca);
for (int i = 0; i < ca; i++) {
int n1, n2;
scanf("%d%d", &n1, &n2);
int nc = 0;
set<int>::iterator it;
for (it = s[n1].begin(); it != s[n1].end(); it++) {
if (s[n2].find(*it) != s[n2].end()) {
nc++;
}
}
int nt = s[n1].size() + s[n2].size()- nc;
double ans = (nc*1.0) / (nt*1.0) * 100;
printf("%.2lf%%\n", ans);
}
}
return 0;
}