• 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;
    }
    

  • 相关阅读:
    博客园电子期刊2009年6月刊发布
    今晚22:30~23:00博客程序更新
    博客园上海俱乐部活动通知(2009613)
    【意见征集补充】09'博客园T恤设计
    C# WinForm webBrowser 内嵌网页的按钮的OnClientClick事件的return false 在webBrowser中绑定onclick事件后 失效 的变通解决办法
    [转]VS2008中开发智能设备程序的一些总结收藏1
    [转]C#访问SQLite数据库
    [转]VS C# 怎么调试调试服务?
    [转]弹出窗口刷新它的父页面后。出现不重新发送信息,则无法刷新网页
    [转]外部css文件中的 BACKGROUNDIMAGE: url(..\image.gif)指定的背景图像无法显示,谁有好主意?
  • 原文地址:https://www.cnblogs.com/houzm/p/12445271.html
Copyright © 2020-2023  润新知