Pat(Advanced Level)Practice--1063(Set Similarity)

Pat1063代码

题目描述:

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%

利用map和set的性质进行筛选,结果最后一个case超时。

代码:

#include<cstdio>
#include<map>
#include<set>
#define N 55

using namespace std;

set<int> s[N];

int main(int argc,char *argv[])
{
	int n;
	int i,j;
	scanf("%d",&n);
	for(i=1;i<=n;i++)
	{
		int m;
		int temp;
		scanf("%d",&m);
		for(j=1;j<=m;j++)
		{
			scanf("%d",&temp);
			s[i].insert(temp);
		}
	}
	int k;
	scanf("%d",&k);
	while(k--)
	{
		map<int,int> m;
		set<int>::iterator p;
		int x,y;
		scanf("%d%d",&x,&y);
		for(p=s[x].begin();p!=s[x].end();p++)
		{
			int index=*p;
			m[index]++;
		}
		for(p=s[y].begin();p!=s[y].end();p++)
		{
			int index=*p;
			m[index]++;
		}
		int count=0;
		map<int,int>::iterator it;
		for(it=m.begin();it!=m.end();it++)
			if(it->second>1)
				count++;
		printf("%.1f%%\n",100*count*1.0/m.size());
	}

	return 0;
}

AC代码:

利用函数求两个集合的交集,然后就可以求出集合的相似性了;

#include<cstdio>
#include<set>
#include<iterator>
#include<algorithm>
#define N 55

using namespace std;

set<int> s[N];

int main(int argc,char *argv[])
{
	int n;
	int i,j;
	scanf("%d",&n);
	for(i=1;i<=n;i++)
	{
		int m;
		int temp;
		scanf("%d",&m);
		for(j=1;j<=m;j++)
		{
			scanf("%d",&temp);
			s[i].insert(temp);
		}
	}
	int k;
	scanf("%d",&k);
	while(k--)
	{
		int x,y;
		int ret,total;
		set<int> ans;
		scanf("%d%d",&x,&y);
		set_intersection(s[x].begin(),s[x].end(),s[y].begin(),s[y].end(),
		inserter(ans,ans.begin()));//求集合交集
		ret=ans.size();//交集元素个数
		total=s[x].size()+s[y].size()-ret;//不重复元素总个数
		printf("%.1f%%\n",100*ret*1.0/total);
	}

	return 0;
}

最后一个case跑了大概130ms;

AC代码:

写一个函数求集合的并集,其实这和上面的方法是一样的,只不过一个是交集,一个是并集;

#include<cstdio>
#include<set>
#include<iterator>
#include<algorithm>
#define N 55

using namespace std;

set<int> s[N];

int GetUnion(int x,int y)//求并集元素的个数,其实algorithm里面有set_union
{                        //这个函数,在这里我们可以简单的实现一下
	int ret=0;
	set<int>::iterator it;
	for(it=s[x].begin();it!=s[x].end();it++)
	{
		if(s[y].find(*it)==s[y].end())
			ret++;
	}
	ret+=s[y].size();
	return ret;
}

int main(int argc,char *argv[])
{
	int n;
	int i,j;
	scanf("%d",&n);
	for(i=1;i<=n;i++)
	{
		int m;
		int temp;
		scanf("%d",&m);
		for(j=1;j<=m;j++)
		{
			scanf("%d",&temp);
			s[i].insert(temp);
		}
	}
	int k;
	scanf("%d",&k);
	while(k--)
	{
		int x,y;
		int ret,total;
		scanf("%d%d",&x,&y);
		total=GetUnion(x,y);
		ret=s[x].size()+s[y].size()-total;
		printf("%.1f%%\n",100*ret*1.0/total);
	}
	return 0;
}

最后一个case跑了大概170ms;

时间: 2024-10-09 06:05:52

Pat(Advanced Level)Practice--1063(Set Similarity)的相关文章

1002 A+B for Polynomials (PAT (Advanced Level) Practice)

This time, you are supposed to find A+B where A and B are two polynomials. Input Specification: Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial: K N?1?? a?N?1???? N?2?? a?N?2?

PAT (Advanced Level) Practice 1011 World Cup Betting (20 分)

With the 2010 FIFA World Cup running, football fans the world over were becoming increasingly excited as the best players from the best teams doing battles for the World Cup trophy in South Africa. Similarly, football betting fans were putting their

PAT (Advanced Level) Practice 1068 Find More Coins

题解 01背包板子 + 记录路径.这次的记录路径比较特殊,要从多组解中找到一组由尽量小价值的硬币组成的解.所以不能利用一维数组记录路径,path[目前重量] = 物品序号,因为这样最后只能记录一个可能符合或不符合要求解.所以应该利用二维数组记录路径,path[ 物品序号 ][ 目前重量 ] = 1,这样可以记录多组解.因为要求为找到最小的一组解,所以先将拥有的硬币从大到小排序,以便于进行01背包时,可以从大到小更新解. 代码 #include<bits/stdc++.h> using name

Pat(Advanced Level)Practice--1043(Is It a Binary Search Tree)

Pat1043代码 题目描述: A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes

Pat(Advanced Level)Practice--1044(Shopping in Mars)

Pat1044代码 题目描述: Shopping in Mars is quite a different experience. The Mars people pay by chained diamonds. Each diamond has a value (in Mars dollars M$). When making the payment, the chain can be cut at any position for only once and some of the diam

PAT (Advanced Level) 1093. Count PAT&#39;s (25)

预处理每个位置之前有多少个P,每个位置之后有多少个T. 对于每个A,贡献的答案是这个A之前的P个数*这个A之后T个数. #include<cstdio> #include<cstring> long long MOD=1e9+7; const int maxn=1e5+10; long long dp1[maxn],dp2[maxn]; char s[maxn]; int main() { scanf("%s",s); memset(dp1,0,sizeof d

PAT (Advanced Level) 1055. The World&#39;s Richest (25)

排序.随便加点优化就能过. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<map> #include<queue> #include<string> #include<stack> #include<vector> using names

Pat(Advanced Level)Practice--1018(Public Bike Management)

Pat1018代码 题目描述: There is a public bike service in Hangzhou City which provides great convenience to the tourists from all over the world. One may rent a bike at any station and return it to any other stations in the city. The Public Bike Management C

Pat(Advanced Level)Practice--1076(Forwards on Weibo)

Pat1076代码 题目描述: Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and may follow many other users as well. Hence a social network is formed with followers relations. When a user makes a post on Weibo, all hi

Pat(Advanced Level)Practice--1016(Phone Bills)

Pat1016代码 题目描述: A long-distance telephone company charges its customers by the following rules: Making a long-distance call costs a certain amount per minute, depending on the time of day when the call is made. When a customer starts connecting a lon