1063. Set Similarity (25)

本文介绍了一个计算两个整数集合相似度的问题,通过定义相似度为两集合共有不同元素数量与总不同元素数量的比例来解决。提供了两种解决方案,一种使用标准 set,另一种使用 multiset,并详细解释了计算过程中的注意事项。

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

  1. Set Similarity (25)
    时间限制
    300 ms
    内存限制
    65536 kB
    代码长度限制
    16000 B
    判题程序
    Standard
    作者
    CHEN, Yue

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%


今天学习力set和multiset,对两者的了解更深一层。
这题刚开始没有理解正确,使用了multiset解题,花了近一个小时。。。
当然最后结果应该是正确的,但是输出竟然出问题了。
原因是因为我是先计算再转化为浮点型,这个过程中可能会丢数。只要先转化浮点型就行了。


#include<iostream>
#include<set>
#include<vector>

using namespace std;
int main()
{
    //freopen("in.txt","r",stdin);
    int N,K;
    cin>>N;
    set <int> m[50];
    for(int i=0;i<N;i++)
    {
        int M,c;
        cin>>M;
        for(int j=0;j<M;j++)
        {
            cin>>c;
            m[i].insert(c);
        }
    }
    cin>>K;
    int m1,m2;
    while(K--)
    {
        cin>>m1>>m2;
        set<int>::const_iterator sm2=m[m2-1].begin();
        int total=m[m1-1].size()+m[m2-1].size();
        int same=0;//相同数的总数
        while(sm2!=m[m2-1].end())
        {
            if(m[m1-1].find(*sm2)!=m[m1-1].end())
                same++;
            sm2++;
        }
         printf("%.1f%%\n", same*1.0/(total-same)*100);  
        //float bili=(100*same)/(total-same);
        //printf("%.1f%%\n",bili);
    }
    system("pause");
    return 0;
}

附上使用mutilset解题的代码,也能输出正确结果,自我感觉思路还是不错的,虽然一开始就走入了岔道。。

#include<iostream>
#include<set>
#include<vector>

using namespace std;
int main()
{
    freopen("in.txt","r",stdin);
    int N,K;
    cin>>N;
    set <int> m[50];
    for(int i=0;i<N;i++)
    {
        int M,c;
        cin>>M;
        for(int j=0;j<M;j++)
        {
            cin>>c;
            m[i].insert(c);
        }
    }
    cin>>K;
    int m1,m2;
    while(K--)
    {
        cin>>m1>>m2;
        set<int>::const_iterator sm1=m[--m1].begin();
        set<int>::const_iterator sm2=m[--m2].begin();
        int total=m[m1].size()+m[m2].size();
        int same=0;//相同数的总数
        int lastsame=-1;
        int samenum=0;////相同数的总类
        while(sm1!=m[m1].end()&&sm2!=m[m2].end())
        {
            if(*sm1==*sm2) 
            {
                if(lastsame!=*sm1)
                {
                    samenum++;
                    lastsame=*sm1;//上一个相同的数
                }
                sm1++;
                sm2++;
                same=same+2;
                continue;
            }
            else
            {
                if(*sm1==lastsame)
                {
                    sm1++;
                    same++;
                    continue;
                }
                else if(*sm2==lastsame)
                {
                    sm2++;
                    same++;
                    continue;
                }
                if(*sm1<*sm2)
                {
                    int mm=*sm1;
                    sm1++;
                    if(mm==*sm1)
                        total--;
                }
                else 
                {
                    int mm=*sm2;
                    sm2++;
                    if(mm==*sm2)
                        total--;
                }
            }
        }
        while(sm2!=m[m2].end())
        {
            if(*sm2==lastsame)
            {
                sm2++;
                same++;
            }
            else 
            {
                int mm=*sm2;
                sm2++;
                if(sm2==m[m2].end())
                    break;
                if(*sm2==mm)
                    total--;
            }
        }

        while(sm1!=m[m1].end())
        {
            if(*sm1==lastsame)
            {
                sm1++;
                same++;
            }
            else 
            {
                int mm=*sm1;
                sm1++;
                if(sm1==m[m1].end())
                    break;
                if(*sm1==mm)
                    total--;
            }
        }
        float bili=100.0*samenum/(total-same+samenum);
        printf("%.1f",bili);cout<<"%"<<endl;
    }

    system("pause");
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值