L2-005 集合相似度 - java

L2-005 集合相似度


时间限制
400 ms
内存限制
64 MB


题目描述:

给定两个整数集合,它们的相似度定义为: N c / N t ∗ 100 % N_{c} / N_{t} * 100\% Nc/Nt100%。其中N c 是两个集合都有的不相等整数的个数, N t N_{t} Nt 是两个集合一共有的不相等整数的个数。你的任务就是计算任意一对给定集合的相似度。

输入格式:
输入第一行给出一个正整数N(≤50),是集合的个数。随后N行,每行对应一个集合。每个集合首先给出一个正整数M(≤ 1 0 4 10^{4} 104 ),是集合中元素的个数;然后跟M个[0, 1 0 9 10^{9} 109 ]区间内的整数。

之后一行给出一个正整数K(≤2000),随后K行,每行对应一对需要计算相似度的集合的编号(集合从1到N编号)。数字间以空格分隔。

输出格式:
对每一对需要计算的集合,在一行中输出它们的相似度,为保留小数点后2位的百分比数字。

输入样例:
3
3 99 87 101
4 87 101 5 87
7 99 101 18 5 135 18 99
2
1 2
1 3
输出样例:
50.00%
33.33%


给定n个集合
再给定k组 要进行计算相似度的两个集合
求出 这两个集合不重复的相同的个数 这两个集合中不重所有个数 \frac{这两个集合不重复的相同的个数}{这两个集合中不重所有个数} 这两个集合中不重所有个数这两个集合不重复的相同的个数


emmmmmmm

set将每个集合弄成不重复的
然后 一个一个去填充到res中

两个集合相同的个数 = 两个集合的个数 - 两个集合合并之后不同的个数
答案就是 两个集合相同的数 两个集合合并之后不同的个数 \frac{两个集合相同的数}{两个集合合并之后不同的个数} 两个集合合并之后不同的个数两个集合相同的数

注: java 会T 数据量有亿点大


暴力

import java.io.*;
import java.math.*;
import java.util.*;

public class Main
{

	public static void main(String[] args)
	{
		int n = sc.nextInt();
		TreeSet<Integer> shu[] = new TreeSet[n + 10];

		for (int i = 1; i <= n; i++)
		{
			int cnt = sc.nextInt();
			shu[i] = new TreeSet<Integer>();
			while (cnt-- > 0)
			{
				int x = sc.nextInt();
				shu[i].add(x);
			}
		}

		int k = sc.nextInt();
		TreeSet<Integer> res = new TreeSet<Integer>();
		while (k-- > 0)
		{
			int a = sc.nextInt();
			int b = sc.nextInt();

			res.clear();
			for (int i : shu[a])
				res.add(i);
			for (int i : shu[b])
				res.add(i);

			out.printf("%.2f%%\n", (shu[a].size() + shu[b].size() - res.size()) * 100.0 / res.size());
		}

		out.flush();
		out.close();
	}

	static Scanner sc = new Scanner(System.in);
	static PrintWriter out = new PrintWriter(System.out);
}


Set 的 addAll函数

import java.io.*;
import java.math.*;
import java.util.*;

public class Main
{

	public static void main(String[] args)
	{
		int n = sc.nextInt();
		TreeSet<Integer> shu[] = new TreeSet[n + 10];

		for (int i = 1; i <= n; i++)
		{
			int cnt = sc.nextInt();
			shu[i] = new TreeSet<Integer>();
			while (cnt-- > 0)
			{
				int x = sc.nextInt();
				shu[i].add(x);
			}
		}

		int k = sc.nextInt();
		TreeSet<Integer> res = new TreeSet<Integer>();
		while (k-- > 0)
		{
			int a = sc.nextInt();
			int b = sc.nextInt();

			res.clear();
			res.addAll(shu[a]);
			res.addAll(shu[b]);

			out.printf("%.2f%%\n", (shu[a].size() + shu[b].size() - res.size()) * 100.0 / res.size());
		}

		out.flush();
		out.close();
	}

	static Scanner sc = new Scanner(System.in);
	static PrintWriter out = new PrintWriter(System.out);
}

c++

#include <cstdio>
#include <unordered_set>

using namespace std;

const int N = 50;
unordered_set<int> shu[N + 10];
unordered_set<int> res;

int main()
{
    int n; scanf("%d", &n);
    for(int i = 1; i <= n; i ++)
    {
        int cnt; scanf("%d", &cnt);
        while(cnt -- > 0)
        {
            int x; scanf("%d", &x);
            shu[i].insert(x); 
        }
    }
    
    int k; scanf("%d", &k);
    while(k -- > 0)
    {
        int a, b; scanf("%d%d", &a, &b);
        res.clear();
        
        for(int i : shu[a]) res.insert(i);
        for(int i : shu[b]) res.insert(i);
        
        printf("%.2f%%\n", (shu[a].size() + shu[b].size() - res.size()) * 100.0 / res.size());
    }
    
    return 0;
}

TreeSet


如果有说错的 或者 不懂的 尽管提 嘻嘻

一起进步!!!


闪现

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

谢谢 啊sir

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值