1063 Set Similarity (25分)【set】

news/2024/7/7 12:04:04

1063 Set Similarity (25分)

Given two sets of integers, the similarity of the sets is defined to be N​c​​/N​t​​×100%, where N​c​​ is the number of distinct common numbers shared by the two sets, and N​t​​ 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 (≤10​4​​) and followed by M integers in the range [0,10​9​​]. 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%

题目大意:

给定N个集合,给出的集合中可能含有相同的值。然后要求M个查询,每个查询给出两个集合的编号x和y,要求集合x和集合y的相同元素率:\frac{N_{c}}{N_{t}}

解题思路:

这里我们可以借助N个STL的set容器来保存这N个set,当判断x和y两个set的相同元素率时,我们只需要遍历这两个set统计其中的重复元素的个数即可。 

#include<iostream>
#include<cstdio>
#include<set>
using namespace std;

set<int> s[60];

double fun(int x, int y)
{
	int cnt = 0;
	for(set<int>::iterator it = s[x].begin();it!=s[x].end();it++)
	{
		if(s[y].find(*it)!=s[y].end())
			cnt++;
	}
	double res = cnt*100.0 / (s[x].size() + s[y].size() - cnt);
	return res;
}

int main()
{
	int n;
	scanf("%d", &n);
	for (int i = 1; i <= n; i++)
	{
		int m;
		scanf("%d", &m);
		for (int j = 0; j < m; j++)
		{
			int num;
			scanf("%d", &num);
			s[i].insert(num);
		}
	}
	int k;
	scanf("%d", &k);
	for (int i = 0; i < k; i++)
	{
		int x, y;
		scanf("%d %d", &x, &y);
		printf("%.1lf%%\n", fun(x, y));
	}
	return 0;
}

 


http://www.niftyadmin.cn/n/4610379.html

相关文章

PostgreSQL 多国语言支持的实现

1、先了解&#xff1a;GNU gettext 2、以 pg_config 为例&#xff0c;打开 src/bin/pg_config/nls.mk # src/bin/pg_config/nls.mk CATALOG_NAME pg_config AVAIL_LANGUAGES cs de es fr it ja ko nb pl pt_BR ro ru sv ta tr zh_CN zh_TW GETTEXT_FILES pg_config.…

1060 Are They Equal (25分)【string】

1060 Are They Equal (25分) If a machine can save only 3 significant digits, the float numbers 12300 and 12358.9 are considered equal since they are both saved as 0.12310​5​​ with simple chopping. Now given the number of significant digits on a machine …

Node.js中的HTTPS示例

需要openssl的支持&#xff0c; openssl本身不提供windows的安装程序&#xff0c;可以按照如下的步骤进行安装&#xff1a; (参考https://conetrix.com/Blog/how-to-install-openssl-on-windows-7&#xff0c;并复制到下面) How-to Install OpenSSL on Windows 7 Download and …

动态规划专题详细总结(常见简单类型)

什么是动态规划 动态规划(Dynamic Programming, DP)是一种用来解决一类最优化问题的算法思想。简单来说&#xff0c;动态规划将一个复杂的问题分解为若干个子问题&#xff0c;通过综合子问题的最优解来得到原问题的最优解。需要注意的是&#xff0c;动态规划会将每个求解过的子…

MySQL数据库无法启动的简单排错

原创作品&#xff0c;允许转载&#xff0c;转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://dgd2010.blog.51cto.com/1539422/1406691 一般来说有经验的管理员在部署操作系统时通常会将操作系统本身与应用软件分离&#xff0c;将两…

7-43 字符串关键字的散列映射 (25 分)

7-43 字符串关键字的散列映射 (25 分) 给定一系列由大写英文字母组成的字符串关键字和素数P&#xff0c;用移位法定义的散列函数H(Key)将关键字Key中的最后3个字符映射为整数&#xff0c;每个字符占5位&#xff1b;再用除留余数法将整数映射到长度为P的散列表中。例如将字符串…

Node.js 开发相关

2019独角兽企业重金招聘Python工程师标准>>> 全局安装默认存放路径npm install express -g C:\Users\Administrator\AppData\Roaming\npm转载于:https://my.oschina.net/u/1179666/blog/1511162

P1216 [USACO1.5][IOI1994]数字三角形 Number Triangles【DP、数塔问题】

题目描述 观察下面的数字金字塔。 写一个程序来查找从最高点到底部任意处结束的路径&#xff0c;使路径经过数字的和最大。每一步可以走到左下方的点也可以到达右下方的点。 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5 在上面的样例中,从 7 \to 3 \to 8 \to 7 \to …