PAT Advanced 1063. Set Similarity (25)

问题描述:

1063. Set Similarity (25)

时间限制
300 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

Given two sets of integers, the similarity of the sets is defined to be Nc/Nt*100%, where Nc is the number of distinct common numbers shared by the two sets, and Nt is 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 first 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 by M integers in the range [0, 109]. After the input of sets, a positive integer K (<=2000) is given, followed by K lines of queries. Each query gives a pair of set numbers (the sets are numbered from 1 to N). All the numbers in a line are separated by a space.

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 99 87 101
4 87 101 5 87
7 99 101 18 5 135 18 99
2
1 2
1 3
Sample Output:
50.0%
33.3%


只要知道stl里的set_intersection()的集合求交操作就行了。。。

需要注意的是,set_intersection()是比set_union()要快很多的,这一题如果用set_union()就运超了。。。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include<bits/stdc++.h>
using namespace std;
int main()
{
// freopen("data.txt","r",stdin);
	int n,m,k,x,x1,x2,c,t;
	scanf("%d",&n);
	vector<set<int> > v;
	for(; n--;)
	{
		scanf("%d",&m);
		set<int> s;
		for(; m--;)
		{
			scanf("%d",&x);
			s.insert(x);
		}
		v.push_back(s);
	}
	scanf("%d",&k);
	set<int> se;
	for(; k--;)
	{
		scanf("%d %d",&x1,&x2);
		set_intersection(v[x1-1].begin(),v[x1-1].end(),v[x2-1].begin(),v[x2-1].end(),inserter(se,se.begin()));
		c=se.size();
		t=(v[x1-1].size()+v[x2-1].size()-c);
		se.clear();
		printf("%.1f%%\n",(double)c*100/t);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/baidu_37550206/article/details/79593967