1063 Set Similarity (25分)

本文介绍了一种计算两个集合相似度的方法,分子为两集合中相同元素的数量(去重),分母为两集合总元素数量(去重)。通过C++代码实现,使用unordered_set存储集合元素,遍历集合进行比较计算。

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%

题意:求两个集合相似度,分子为相同的元素个数(去重),分母为总共的元素个数(去重)

#include<unordered_set>
#include<iostream>
#include<vector>

using namespace std;

int main(){

    int n;
    cin >> n;
    vector<unordered_set<int> > vec (n + 1);
    for(int i = 1; i <= n; i++){
        int m;
        scanf("%d", &m);
        for(int j = 0; j < m; j++){
            int temp;
            scanf("%d", &temp);
            vec[i].insert(temp);

        }


    }
    int k;
    cin >> k;
    for(int i = 0; i < k; i++){

        int a, b;
        scanf("%d %d", &a, &b);
        int nc = 0;
        int nt = 0;
        for(auto it : vec[a]){
    
            if(vec[b].find(it) != vec[b].end()){
                nc++;
            } 
        }
        nt = vec[a].size() + vec[b].size() - nc;
        float result = 1.0 * nc / nt * 100;
        printf("%.1f%%\n", result);
    }
    return 0;
}

 

先展示下效果 https://pan.quark.cn/s/a4b39357ea24 遗传算法 - 简书 遗传算法的理论是根据达尔文进化论而设计出来的算法: 人类是朝着好的方向(最优解)进化,进化过程中,会自动选择优良基因,淘汰劣等基因。 遗传算法(英语:genetic algorithm (GA) )是计算数学中用于解决最佳化的搜索算法,是进化算法的一种。 进化算法最初是借鉴了进化生物学中的一些现象而发展起来的,这些现象包括遗传、突变、自然选择、杂交等。 搜索算法的共同特征为: 首先组成一组候选解 依据某些适应性条件测算这些候选解的适应度 根据适应度保留某些候选解,放弃其他候选解 对保留的候选解进行某些操作,生成新的候选解 遗传算法流程 遗传算法的一般步骤 my_fitness函数 评估每条染色体所对应个体的适应度 升序排列适应度评估值,选出 前 parent_number 个 个体作为 待选 parent 种群(适应度函数的值越小越好) 从 待选 parent 种群 中随机选择 2 个个体作为父方和母方。 抽取父母双方的染色体,进行交叉,产生 2 个子代。 (交叉概率) 对子代(parent + 生成的 child)的染色体进行变异。 (变异概率) 重复3,4,5步骤,直到新种群(parentnumber + childnumber)的产生。 循环以上步骤直至找到满意的解。 名词解释 交叉概率:两个个体进行交配的概率。 例如,交配概率为0.8,则80%的“夫妻”会生育后代。 变异概率:所有的基因中发生变异的占总体的比例。 GA函数 适应度函数 适应度函数由解决的问题决定。 举一个平方和的例子。 简单的平方和问题 求函数的最小值,其中每个变量的取值区间都是 [-1, ...
### Halcon 中的相似性匹配功能 在 Halcon 中,相似性匹配是一种用于识别图像中特定模式的技术。该技术通过模板匹配的方式,在目标图像内寻找与给定模型最接近的部。为了实现这一目的,Halcon 提供了一系列算子来创建和应用这些模型。 #### 创建相似性模型 要执行相似性匹配,首先需要定义一个或多个感兴趣区域作为模板,并基于此构建模型。这可以通过 `create_similarity_model` 函数完成: ```cpp // 定义模板窗口并读取模板图像 read_image (Image, 'template_path') reduce_domain (Image, Region, ImageReduced) // 使用默认参数创建相似度量模型 create_similarity_model (ImageReduced, 0.5, 0.7, 'use_polarity', \ 'gauss', 3, 1, SimilarityModelID) ``` 上述代码片段展示了如何加载一幅图片到内存中,并将其指定为模板的一部;随后调用 `create_similarity_model()` 来初始化一个新的相似性检测器实例[^2]。 #### 应用相似性模型进行搜索 一旦建立了模型之后,则可以利用它来进行实际的目标查找工作。下面的例子说明了怎样在一个新输入的画面里定位之前设定好的对象位置: ```cpp // 加载待处理的新图象 read_image (SceneImage, 'scene_path') // 执行相似性匹配操作 find_all_similar_objects (SimilarityModelID, SceneImage, MinScore, MaxOverlap,\ NumMatches, RowBegin, ColumnBegin, Angle, ScaleRow, \ ScaleColumn, Score, HandleArray) ``` 这里的关键在于函数 `find_all_similar_objects()`, 这个命令会遍历整个场景图像去发现所有可能存在的匹配项,并返回它们各自的坐标以及其他相关信息如角度旋转和平移缩放因子等属性值[^3]。 #### 结果可视化 最后一步通常是将找到的结果直观地显示出来以便验证效果好坏与否: ```cpp dev_display(SceneImage); for i := 0 to |HandleArray|-1 by 1 do dev_set_color ('red'); gen_rectangle1 (RowBegin[i], ColumnBegin[i],\ RowBegin[i]+ScaleRow[i]*TemplateHeight,\ ColumnBegin[i]+ScaleColumn[i]*TemplateWidth); endfor; ``` 这段脚本会在原始画面上绘制红色矩形框标记出每一个被成功辨认出来的物体轮廓边界线[^1]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值