PAT ~ L2-005. 集合相似度 (set)

思路:直接利用algorithm库中的集合的交操作即可。并集元素个数的等于两个集合元素个数相加 - 交集元素个数。

但是注意不能在直接求并集了,太浪费时间,会超时。

不知道集合的交并补差对应的函数的,可以参考我之前的一篇博客set相关,在中间那一部分

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 55;
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
int n;
set<int> s[MAXN];
int main()
{
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
    {
        int m; scanf("%d", &m);
        while (m--)
        {
            int x; scanf("%d", &x);
            s[i].insert(x);
        }
    }
    int k;
    scanf("%d", &k);
    while (k--)
    {
        int a, b;
        scanf("%d%d", &a, &b);
        set<int> ans1, ans2;
        set_intersection(ALL(s[a]), ALL(s[b]), INS(ans1));
        //set_union(ALL(s[a]), ALL(s[b]), INS(ans2)); //会超时
        double nc = ans1.size(), nt = s[a].size() + s[b].size() - ans1.size();
        printf("%.2f%%\n", nc / nt * 100.0);
    }
    return 0;
}
/*
3
3 99 87 101
4 87 101 5 87
7 99 101 18 5 135 18 99
2
1 2
1 3
*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值