集合相似度

题目描述

给定两个整数集合,两个集合的相似度定义为 Nc/Nt×100%,其中 Nc 是两个集合中都存在的不同整数的数量,Nt 是两个集合中不同整数的数量。

现在,请你计算给定集合的相似度。

输入格式

第一行包含整数 N,表示集合数量。

接下来 N 行,每行包含一个集合的信息,首先包含一个整数 M,表示集合中的数的个数,然后包含 M 个整数,表示集合中的每个元素。

再一行,包含整数 K,表示询问次数。

接下来 K 行,每行包含一组询问,包括两个整数 ab,表示询问集合 a 和集合 b的相似度。

所有集合的编号为 1∼N

输出格式

每行输出一个询问的结果,保留一位小数。

输出答案与标准答案的绝对误差不超过 0.10.1 即视为正确。

数据范围

1≤N≤50,

1≤M≤10000,

1≤K≤2000,

集合中的元素取值均在 [0,109][0,109] 范围内。

输入样例:

3
3 99 87 101
4 87 101 5 87
7 99 101 18 5 135 18 99
2
1 2
1 3

输出样例:

50.0%
33.3%   

这道题我用到了两种方法,一种是哈希表一种是数组。

哈希表

#include<iostream>
#include<bits/stdc++.h>
#include<map>
#define N 100
using namespace std;
int main(){
    unordered_set<int> mp[N];
    int n,m,k,a,b,sum,ans=0;
    double d;
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>m;
        while(m--){
            cin>>a;
            mp[i].insert(a);
        }
    }
    cin>>k;
    while(k--){
        ans=0;
        cin>>a>>b;
        for(unordered_set<int>::iterator it=mp[a].begin();it!=mp[a].end();it++){
            if(mp[b].count(*it))
            ans++;
        }
        sum=mp[b].size()+mp[a].size()-ans;
        if(ans>sum)
        d=1;
       else
        d=(ans*1.0)/sum;
        d=d*100;
        cout<<fixed<<setprecision(1)<<d<<"%"<<endl;
    }
    return 0;
} 

数组

这种方法时间复杂度较高不建议使用

#include<iostream>
#include<map>
#include<bits/stdc++.h>
const int N=1e4+10;
using namespace std;
int str[N][N];
int main(){
    int n,m,a,b,sum=0,ans=0;
    double d;
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>str[i][0];
        for(int j=1;j<=str[i][0];j++){
            cin>>str[i][j];
        }
    }
    cin>>n;
    while(n--){
        map<int,int> mp,arr;
        sum=0;ans=0;
        cin>>a>>b;
        for(int i=0;i<=str[a][0];i++){
            if(mp[str[a][i]]==0)
            mp[str[a][i]]++; 
        }
        for(int i=0;i<=str[b][0];i++){
            if(mp[str[b][i]]==1){
                ans++;
                mp[str[b][i]]=2;
            }
            if(mp[str[b][i]]==0){
                sum++;
                mp[str[b][i]]=-1;
            }
            if(arr[str[b][i]]==0)
            arr[str[b][i]]++;
        }
        for(int i=0;i<=str[a][0];i++){
            if(arr[str[a][i]]==0){
                sum++;
                arr[str[a][i]]=3;
            }
        }
        if(ans>sum)
        d=100;
        else{
        d=(ans*1.0)/sum;
        d=d*100;
        }
        cout<<fixed<<setprecision(1)<<d<<"%"<<endl;
    } 
    return 0;
}

关于这两道题的写法已经讲解完毕,希望对各位有用,谢谢大家

### 计算集合相似度的算法实现 #### 背景说明 为了计算两个集合之间的相似度,通常采用一种基于交集与并集的比例方法。具体而言,通过统计两集合中共有元素的数量 \( N_c \),以及两集合总的不同元素数量 \( N_t \),可以得出其相似度比例。 相似度公式如下: \[ \text{Similarity} = \frac{N_c}{N_t} \times 100\% \] 其中, - \( N_c \) 表示两集合共有不同整数的个数; - \( N_t \) 表示两集合一共含有的不同整数的个数。 此公式的理论基础已在站内引用中提及[^1]。 --- #### Python 实现代码 以下是使用 Python 编写的集合相似度计算函数: ```python def calculate_similarity(set_a, set_b): # 将输入列表转换为集合以去除重复项 unique_set_a = set(set_a) unique_set_b = set(set_b) # 计算交集和并集 intersection_count = len(unique_set_a.intersection(unique_set_b)) # 共同元素个数 Nc union_count = len(unique_set_a.union(unique_set_b)) # 总共不同元素个数 Nt # 如果并集为空,则返回 0%,防止除零错误 if union_count == 0: return 0.0 # 计算相似度百分比 similarity_percentage = (intersection_count / union_count) * 100 return round(similarity_percentage, 2) # 返回保留两位小数的结果 # 测试用例 set_a = [1, 2, 3, 4, 5] set_b = [3, 4, 5, 6, 7] similarity_result = calculate_similarity(set_a, set_b) print(f"The similarity between the two sets is {similarity_result}%.") ``` 上述代码实现了以下功能: 1. **去重处理**:将输入的列表转化为集合,自动移除重复元素。 2. **交集与并集运算**:利用 `set` 的内置方法 `.intersection()` 和 `.union()` 来分别获取共同元素和全部唯一元素。 3. **异常处理**:当两集合均为空时,避免因分母为零而导致程序崩溃。 4. **结果精度控制**:最终结果四舍五入至两位小数。 --- #### Java 实现代码 如果需要在 Java 中实现该逻辑,可参考以下代码片段: ```java import java.util.HashSet; import java.util.Set; public class SetSimilarity { public static double calculateSimilarity(int[] arrayA, int[] arrayB) { // 创建 HashSet 并填充数据 Set<Integer> setA = new HashSet<>(); for (int num : arrayA) { setA.add(num); } Set<Integer> setB = new HashSet<>(); for (int num : arrayB) { setB.add(num); } // 获取交集和并集 Set<Integer> intersection = new HashSet<>(setA); intersection.retainAll(setB); // 只保留两者都存在的元素 Set<Integer> union = new HashSet<>(setA); union.addAll(setB); // 合并两者的所有元素 // 防止除零错误 if (union.size() == 0) { return 0.0; } // 计算相似度 return ((double) intersection.size() / union.size()) * 100; } public static void main(String[] args) { int[] setA = {1, 2, 3, 4, 5}; int[] setB = {3, 4, 5, 6, 7}; double result = calculateSimilarity(setA, setB); System.out.printf("The similarity between the two sets is %.2f%%.\n", result); } } ``` Java 版本同样遵循了相同的逻辑框架,并提供了更详细的注释以便于理解。 --- #### 注意事项 1. 输入的数据应先经过预处理,确保不会包含非法字符或其他非数值类型的干扰因素。 2. 当集合完全一致时,\( N_c = N_t \),此时相似度为 100%;而当无任何公共元素时,相似度则降为 0%。 3. 对于大规模数据集,需考虑性能优化策略,例如减少不必要的内存占用或提高哈希表查找效率。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值