PAT 1063. Set Similarity (25)

Source: Internet
Author: User
Tags stdin

http://pat.zju.edu.cn/contests/pat-a-practise/1063

Given two sets of integers, the similarity of the sets is defined to being nc/nt*100%, where Nc is the number of distinct COM Mon numbers shared by the two sets, and Nt are the total number of distinct numbers in the two sets. Your job is to calculate the similarity of any given pair of sets.

Input Specification:

Each input file contains one test case. Each case, gives a positive integer N (<=50) which is the total number of sets. Then N lines Follow, each gives a set with a positive m (<=104) and followed through m integers in the range [0, 109]. After the input of sets, a positive an integer K (<=2000) is given, followed by K lines of queries. Each query gives a pair's set numbers (the sets are numbered from 1 to N). All of the numbers in a line are separated by a.

Output Specification:

For each query, print in one line the similarity of the sets, in the percentage form accurate up to 1 decimal place. Sample Input:

3
3,
4 5, 7, 5
135
, 2 1 2 1 3
Sample Output:
50.0%
33.3%

#include <cstdio>
#include <set>
using namespace std;
Set<int> s[50];
int main () {
	//freopen ("g:\\input.in", "R", stdin);
	int n;
	scanf ("%d", &n);
	for (int i = 0; i < n; ++i) {
		int num, x;
		For (scanf ("%d", &num); num;--num) {
			scanf ("%d", &x);
			S[i].insert (x);
		}
	}
	int k;
	For (scanf ("%d", &k); k;--k) {
		int A, b;
		scanf ("%d%d", &a, &b);
		int counta = S[a-1].size (), COUNTB = S[b-1].size ();
		int cnt = 0;
		for (Set<int>::iterator iter = S[a-1].begin (); Iter!= s[a-1].end (); ++iter) {
			if (S[b-1].find (*iter)!= s [B-1].end ())
				cnt++;
		}
		printf ("%.1lf%%\n", cnt*1.0/(counta+countb-cnt) *100);
	}
	return 0;
}


Using STL set_intersection Set_union:

#include <cstdio>
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <iterator>
#include <set>
using namespace std;
Set<int> s[50];
int main () {
	//freopen ("g:\\input.in", "R", stdin);
	int n;
	scanf ("%d", &n);
	for (int i = 0; i < n; ++i) {
		int num, x;
		For (scanf ("%d", &num); num;--num) {
			scanf ("%d", &x);
			S[i].insert (x);
		}
	}
	int k;
	For (scanf ("%d", &k); k;--k) {
		int A, b;
		scanf ("%d%d", &a, &b);
		set<int> Inter, Uni;
		Set_intersection (S[a-1].begin (), S[a-1].end (), S[b-1].begin (), S[b-1].end (), Inserter (Inter, Inter.begin ()));
		Set_union (S[a-1].begin (), S[a-1].end (), S[b-1].begin (), S[b-1].end (), Inserter (uni, Uni.begin ()));
		cout << setiosflags (ios::fixed) << setprecision (1) << inter.size () *100.0/(S[a-1].size () + S[b-1].siz E ()-inter.size ()) << "%" << Endl;
	}
	return 0;
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.