Set Similarity 笔记

本文介绍了一种计算两个整数集合相似度的方法,通过Nc/Nt*100%的公式,其中Nc是两个集合中共同不同整数的数量,Nt是两个集合中所有不同整数的总数。详细讲解了如何使用C++编程实现这一计算,包括使用set和vector的数据结构,以及如何优化查找过程。

Set Similarity笔记

0.题目概述

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.

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 (<=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.

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%

1.AC代码

#include<iostream>
#include<set>
#include<vector>
using namespace std;
#define N 10000
int main()
{
    int n;
    int num;
    vector<set<int> > v(10000);
    vector<set<int> >::iterator it1;
    set<int>::iterator it2;
    int val;
    int k=0;
    int xb1,xb2;
    int i,j;
    double answer;
    int Nc,Nt;
    set<int> s;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
    	s.clear();
        scanf("%d",&num);
        k=0;
        for(j=0;j<num;j++)
        {
            
            scanf("%d",&val);
         	 s.insert(val);       
        } 
		v[i]=s;
    }
    scanf("%d",&k);
    for(i=0;i<k;i++)
    {
    	scanf("%d%d",&xb1,&xb2);
    	xb1--;xb2--;
    	Nc=0;
    	for(it2=v[xb1].begin();it2!=v[xb1].end();it2++)
    	{
    		if(      ( v[xb2].find(*it2)!=v[xb2].end() )  ==1        )
    		{
    			Nc++;
			}
		}
		Nt=v[xb1].size()+v[xb2].size()-Nc;//观察易得。 
		answer=(double)Nc*100/Nt;
		printf("%.1f%%\n",answer);
	}
    return 0;
}

2.除法,为什么 200/6得不到33.3%而得到的是33.00% ?

因为answer=Nc/Nt;Nc和Nt都是int型,即使Nc=100count21.0 也不能改变int型这个事实。
要得到33.3%,本质上要 200*1.0/6才可以。
1.不要打印时写除法,这样忘记加括号容易错 2.answer=(int1)*1.0/(int2);

3.isRep函数。也许可以改一改,改成set。

int isRep(int *arr,int n,int val)
{
    int i;
    for(i=0;i<n;i++)
    {
        if(arr[i]==val)
            return 1;
    }
    return 0;
}

//改成set
for(j=0;j<num;j++)
{
            
    scanf("%d",&val);
    s.insert(val);       
} 

4.学会了map的 m.clear(),利用标记来找两个一位数组中相同元素的个数,但是还是要遍历,花费时间,可以换作是在set中用find寻找。

 for(i=0;i<k;i++)
    {
    	count2=0;
    	M.clear();
        scanf("%d%d",&pair[0],&pair[1]);
        pair[0]--;pair[1]--;
        for(j=0;j< count[ pair[0]] ;j++ )//利用count[i]存储数组arr[i]元素的个数。
        {
        	M[arr[pair[0]][j]]=1;
		}
		
		for(j=0; j< count[ pair[1]] ;j++)
		{
			if(M[arr[pair[1]][j]]==1)
			{
				count2++;
			}	
		}
		Nc=count2*100*1.0;//必须要加个小数点,0. //不管怎样,这个还是个整数。  1.不要打印时写在一坨 2.两个整数int相除,分子必须*1.0
		Nt=(count[ pair[1] ]+count[  pair[0]  ])-count2;
		answer=Nc*1.0/Nt;
		printf("%.1f%%\n", answer);
    }

5.vector的内容是 set类型

4.1定义:

    vector<set<int> > v(2);//开始出错就是因为vector不会自动变大,自己需要用v.resize(9)来变大,并且初始化了。
    vector<set<int> >::iterator it1;
    set<int>::iterator it2;
    在

4.2 把set内容装入一维动态数组vector

for(i=0;i<n;i++)
    {
    	set<int> s;//必须在这里定义,相当于刷新了,也可以在for外定义set<int> s,这里用一个s.clear()就OK了。
        scanf("%d",&num);
        k=0;
        for(j=0;j<num;j++)
        {
            
            scanf("%d",&val);
         	 s.insert(val);       
        } 
		v[i]=s;
    }

4.3二重循环输出:

for(i=0;i<v.size();i++)
    {
    	for(it2=v[i].begin();it2!=v[i].end();it2++)
    	{
    		cout<<*it2<<' ';
		}
		cout<<endl;
	}
//区别:vector的v可以当成数组用,但是set的s遍历需要迭代器。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值