题目翻译:
按照题目要求计算两两集合之间的相似度
题解思路:
直接调用STL的set_intersection和set_union求交集和并集即可
代码:
#include<bits/stdc++.h>
using namespace std;
int N, K;//集合数量,要搜寻的集合总数
int main()
{
vector<set<int>> sets;
cin >> N;
for (int i = 0;i < N;i++){
int temp;cin >> temp;
set<int> a;
while (temp--){
int k;
cin >> k;
a.insert(k);
}
sets.push_back(a);
}
cin >> K;
for (int i = 0;i < K;i++){
int set_1, set_2;
cin >> set_1 >> set_2;
set<int> temp_cha, temp_union;
set_intersection(sets[set_1 - 1].begin(), sets[set_1 - 1].end(), sets[set_2 - 1].begin(), sets[set_2 - 1].end(), inserter(temp_cha, temp_cha.begin()));
set_union(sets[set_1 - 1].begin(), sets[set_1 - 1].end(), sets[set_2 - 1].begin(), sets[set_2 - 1].end(), inserter(temp_union, temp_union.begin()));
double similarity_rate = double(temp_cha.size()) / temp_union.size() * 100;
cout << fixed << setprecision(1) << similarity_rate << "%" << endl;
}
}
坑点:
无