给定两个整数集合,它们的相似度定义为:Nc/Nt*100%。其中Nc是两个集合都有的不相等整数的个数,Nt是两个集合一共有的不相等整数的个数。你的任务就是计算任意一对给定集合的相似度。
输入格式:
输入第一行给出一个正整数N(<=50),是集合的个数。随后N行,每行对应一个集合。每个集合首先给出一个正整数M(<=104),是集合中元素的个数;然后跟M个[0, 109]区间内的整数。
之后一行给出一个正整数K(<=2000),随后K行,每行对应一对需要计算相似度的集合的编号(集合从1到N编号)。数字间以空格分隔。
输出格式:
对每一对需要计算的集合,在一行中输出它们的相似度,为保留小数点后2位的百分比数字。
输入样例:
3
3 99 87 101
4 87 101 5 87
7 99 101 18 5 135 18 99
2
1 2
1 3
输出样例:
50.00%
33.33%
这题我遇到了一个比较怪的地方。之前我用的是map存每组数是否存在,后来的搜索用map[a]查询,发现最后一组样例超时。但是用set.find(a)就不超时,不太明白为什么。百度说map和set都是红黑树实现的,效率应该是一样的。附ac代码:
1 #include <cstdio>
2 #include <cstring>
3 #include <string>
4 #include <iostream>
5 #include <algorithm>
6 #include <vector>
7 #include <map>
8 #include <queue>
9 #include <map>
10 #include <cmath>
11 #include <set>
12 using namespace std;
13 const int maxn = 111;
14 //map<int,map<int,int> >mp;
15 set<int>st[maxn];
16 set<int>::iterator it;
17
18 int main()
19 {
20 int n;
21 int a,m;
22 scanf("%d",&n);
23 for(int i=1;i<=n;++i)
24 {
25 scanf("%d",&m);
26 for(int j=0;j<m;++j)
27 {
28 scanf("%d",&a);
29 st[i].insert(a);
30 // mp[i][a]=1;
31 }
32 }
33 int k;
34 scanf("%d",&k);
35 int b;
36 for(int i=0;i<k;++i)
37 {
38
39 scanf("%d%d",&a,&b);
40 int nk=st[a].size()+st[b].size(),nc=0;
41 for( it=st[a].begin();it!=st[a].end();++it)
42 {
43 if(st[b].find(*it)!=st[b].end()) nc++; //if(mp[b][*it]==1) nc++;会超时
44 }
45 printf("%.2lf%%n",nc*1.0/(nk-nc)*100);
46 }
47 return 0;
48 }
View Code
原文链接: https://www.cnblogs.com/zmin/p/8529880.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/270100
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!