1063. Set Similarity (25)

1063. Set Similarity (25)

时间限制
300 ms
内存限制
32000 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%
解题思路:
输入数组后,根据输入的数组序号对数组进行排序,然后去重,再进行类似于merge操作的遍历,获得两个数组中不同的数的个数。

#include <iostream>
#include"stdio.h"
#include"stdlib.h"
#include"algorithm"
using namespace std;


struct store{
    int a[10000];
    int length;
    bool sort_flag;//标记数组是否已经排序,可以避免重复排序
    int cnt;
};
int main()
{
    int N,M,K;
    struct store s[50];
    scanf("%d",&N);
    for(int i=0;i<N;i++){
        scanf("%d",&M);
        s[i].length = M;
        s[i].cnt=M;
        s[i].sort_flag = false;
        for(int j=0;j<M;j++){
            scanf("%ld",&s[i].a[j]);
        }
    }
    scanf("%d",&K);

    int first,second;
    for(int k=0;k<K;k++){
        scanf("%d%d",&first,&second);
        first--;
        second--;
        if(!s[first].sort_flag){
            sort(s[first].a,s[first].a+s[first].length);//快排
            //去重
            for(int i=s[first].length-1;i>=1;i--){
                if(s[first].a[i]==s[first].a[i-1]){
                    s[first].a[i]=-1;
                    s[first].cnt--;//记下数组中distinct的数的个数
                }
            }
            s[first].sort_flag=true;//已经排序了
        }


        if(!s[second].sort_flag){
            sort(s[second].a,s[second].a+s[second].length);
            for(int i=s[second].length-1;i>=1;i--){
                if(s[second].a[i]==s[second].a[i-1]){
                    s[second].a[i]=-1;
                    s[second].cnt--;
                }
            }
            s[second].sort_flag=true;
        }
        int distinct=0;
        int i=0,j=0;
        while(i<s[first].length&&j<s[second].length){
            if(s[first].a[i]!=-1&&s[second].a[j]!=-1){
                if(s[first].a[i]<s[second].a[j]){
                    distinct++;
                    i++;
                }
                else if(s[first].a[i]>s[second].a[j]){
                    distinct++;
                    j++;
                }
                else{
                    i++;
                    j++;
                }
            }else{
                if(s[first].a[i]==-1&&s[second].a[j]!=-1){
                    i++;
                }else if(s[first].a[i]!=-1&&s[second].a[j]==-1){
                    j++;
                }
                else /*if(s[first].a[i]!=-1&&s[second].a[j]!=-1)*/{//用else if会超时
                    i++;
                    j++;
                }
            }
        }
        while(i<s[first].length){
            if(s[first].a[i]!=-1)
                distinct++;
            i++;
        }
        while(j<s[second].length){
           if(s[second].a[j]!=-1)
                distinct++;
            j++;
        }

        long common = (s[first].cnt+s[second].cnt-distinct)/2;
        printf("%.1f%\n",(common*100.0/(common+distinct)));
    }
    return 0;
}




posted @ 2013-09-28 14:08  bingtel  阅读(122)  评论(0编辑  收藏  举报