Set Similarity (25)

本文介绍了一个计算两个整数集合相似度的问题,通过编程实现来计算两个集合共有元素占总不重复元素的比例,并提供了C++和Java两种语言的实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述

Given two sets of integers, the similarity of the sets is defined to be Nc/Nt*100%, where Nc is the number of distinct common numbers shared by the two sets, and Nt is the total number of distinct numbers in the two sets.  Your job is to calculate the similarity of any given pair of sets.

输入描述:

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 (<=104) and followed by M integers in the range [0, 109].  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.


输出描述:

For each query, print in one line the similarity of the sets, in the percentage form accurate up to 1 decimal place.

输入例子:

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%

我的代码(C++):

#include<iostream>
#include<set>
using namespace std;
int main()
{
    set<int> s[50];
    set<int>::iterator it;
    int x,y,z,n,m1,m2,i;
    scanf("%d",&x);
    for(i=0;i<x;i++)
    {
        scanf("%d",&y);
        while(y--)
        {
            scanf("%d",&z);
            s[i].insert(z);
        }
    }
    scanf("%d",&n);
    while(n--)
    {
        scanf("%d%d",&m1,&m2);
        int cnt=0;
        for(it=s[m1-1].begin();it!=s[m1-1].end();it++)
        {
            if(s[m2-1].find(*it)!=s[m2-1].end()) cnt++;
        }
        double sum=s[m1-1].size()+s[m2-1].size()-cnt;
        printf("%.1f%%\n",cnt/sum*100);
    }
    return 0;
}

我的代码(Java):

import java.util.HashSet;
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner In=new Scanner(System.in);
		int x=In.nextInt();
		HashSet[] s=new HashSet[x];
		for(int i=0;i<x;i++)
		{
			int y=In.nextInt();
			s[i]=new HashSet<Integer>(y);
			for(int j=0;j<y;j++)
			{
				int z=In.nextInt();
				s[i].add(z);
			}
		}
		int n=In.nextInt();
		for(int i=0;i<n;i++)
		{
			int m1=In.nextInt();
			int m2=In.nextInt();
			int cnt=0;
			HashSet<Integer> s1=s[m1-1];
			HashSet<Integer> s2=s[m2-1];
			for(Integer integer:s2)
			{
				if(s1.contains(integer)) cnt++;
			}
			double sum=s1.size()+s2.size()-cnt;
			System.out.printf("%.1f%%\n",cnt/sum*100);
		}
	}
}
### 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]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值