zoukankan      html  css  js  c++  java
  • PAT Advanced 1063 Set Similarity (25分) [集合set,STL的使⽤]

    题目

    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]. Afer 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存储集合,可以保证数据的唯一不重复,方便之后的统计,设选择的两个集合为a,b
    遍历b中元素,在a中查找是否已经存在

    • 若已存在,说明是交集不重复元素,交集不重复元素个数+1
    • 若不存在,说明是非交集不重复元素,总不重复元素个数+1

    易错点

    最后一个节点超时:若使用set.insert(i).second==false方法添加b中元素到a中并统计交集不重复元素和总不重复元素,会导致超时(可能是因为insert之后还要在set中保持有序)

    知识点

    • %用%转义
    • 用转义
    • set如何判断insert时已经存在 dt.insert(tc).second==false 已经存在于set,插入失败,first为tc对应的迭代器 、

    Code

    AC 代码

    #include <iostream>
    #include <set>
    using namespace std;
    const int maxn=55;
    set<int> as[maxn];
    int main(int arg,char * argv[]) {
    	int n,m,a,t,b,c;
    	scanf("%d",&n);
    	for(int i=1; i<=n; i++) {
    		scanf("%d",&m);
    		for(int j=0; j<m; j++) {
    			scanf("%d",&a);
    			as[i].insert(a);
    		}
    	}
    	scanf("%d",&t);
    	for(int i=0; i<t; i++) {
    		scanf("%d %d",&b,&c);
    		set<int> cs = as[c];
    		int cn=0,an=as[b].size();
    		set<int> dt(as[b]);
    		for(set<int> :: iterator tc=cs.begin(); tc!=cs.end(); tc++) {
    			if(dt.find(*tc)!=dt.end()) cn++;
    			else an++; 
    		}
    		printf("%.1f%%
    ",cn*100.0/an);
    	}
    
    	return 0;
    }
    

    超时代码(仅供对比学习)

    #include <iostream>
    #include <set>
    using namespace std;
    const int maxn=55;
    set<int> as[maxn];
    int main(int arg,char * argv[]) {
    	int n,m,a,t,b,c;
    	scanf("%d",&n);
    	for(int i=1; i<=n; i++) {
    		scanf("%d",&m);
    		for(int j=0; j<m; j++) {
    			scanf("%d",&a);
    			as[i].insert(a);
    		}
    	}
    	scanf("%d",&t);
    	for(int i=0; i<t; i++) {
    		scanf("%d %d",&b,&c);
    		set<int> cs = as[c];
    		int cn=0,an=as[b].size();
    		set<int> dt(as[b]);
    		for(set<int> :: iterator tc=cs.begin(); tc!=cs.end(); tc++) {
    			if(dt.insert(*tc).second==false) cn++;
    			else an++; 
    		}
    		printf("%.1f%%
    ",cn*100.0/an);
    	}
    	return 0;
    }
    

  • 相关阅读:
    destoon手机端mobileurl函数增加城市分类参数
    jCarousel,jQuery下的滚动切换传送插件
    jQuery plugin : bgStretcher 背景图片切换效果插件
    jquery图片切换插件jquery.cycle.js参数详解
    destoon 后台管理左侧新增菜单项
    destoon 列表页面增加手动选择排序方式
    jQuery 淡入淡出有png图的时候 ie8下有黑色边框
    java中Array和ArrayList区别
    趣味理解ADO.NET对象模型
    两个datatable的比较
  • 原文地址:https://www.cnblogs.com/houzm/p/12445271.html
Copyright © 2011-2022 走看看