集合相似度-easy-stl-set


解法一:

#include
#include
#include
#define MAXSIZE 51
using namespace std;
set s[MAXSIZE];/*建立set数组,自动除去重复的并且升序排好,然后利用count函数即可,知识点:stl-set*/
double Same(int t1, int t2) {
	double result = 0.0;
	int counter = 0;
	for (auto it = s[t1].begin(); it != s[t1].end(); ++it) {
		if (s[t2].count(*it))//count函数
			++counter;
	}
	result = (counter * 1.0) / (s[t1].size() + s[t2].size() - counter);
	return result;
}
int main() {
	int N, K;
	cin >> N;
	for (int i = 1, num; i <= N; ++i) {
		cin >> num;
		for (int j = 0, temp; j < num; ++j) {
			cin >> temp;
			s[i].insert(temp);
		}
	}
	cin >> K;
	for (int i = 0, t1, t2; i < K; ++i) {
		cin >> t1 >> t2;
		printf("%.2f%%\n", 100 * Same(t1, t2));
	}
	return 0;
}