集合相似度

#include 
#include 
#include 
#include 
using namespace std;
//3
//3 99 87 101
//4 87 101 5 87
//7 99 101 18 5 135 18 99
//2
//1 2
//1 3
int main() {
	int N, M;
	cin >> N;
	set s[50];
	for (int i = 0; i < N; i++) {
		scanf("%d", &M);
		for (int j = 0, a; j < M; j++) {
			cin >> a;
			s[i].insert(a);
		}
	}
	int k;
	scanf("%d", &k);
	for (int i = 0, a, b; i < k; i++) {
		scanf("%d%d", &a, &b);
		a -= 1, b -= 1;
	    int cnt = s[a].size() + s[b].size();
		/*set l;
		for (auto x : s[a]) l.insert(x);
		for (auto x : s[b]) l.insert(x);
		int sum = l.size();*/
		int sum = 0;
		for (auto x : s[a]) {
			// s[b].count(x) 比 count(s[b].begin(), s[b].end(), x) 效率更高(实测)
			sum += s[b].count(x);
		}
		printf("%.2lf%%\n", (double)sum / (cnt - sum) * 100);
	}
	return 0;
}

你可能感兴趣的:(算法题,算法,c++,数据结构)