2

2022寒假精进训练-8

 2 years ago
source link: https://hbuacm.github.io/2022/02/15/%E3%80%902022%E5%AF%92%E5%81%87%E7%B2%BE%E8%BF%9B%E8%AE%AD%E7%BB%83-8%E3%80%917-10%20%E9%9B%86%E5%90%88%E7%9B%B8%E4%BC%BC%E5%BA%A6/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

HBUACM

【2022寒假精进训练-8】7-10 集合相似度
发表于 2022-02-15| 更新于 2022-02-15|2022寒假精进程序设计训练2022寒假精进训练-8
阅读量:21

给定两个整数集合,它们的相似度定义为:N$_c$/N$_t$×100%。其中N$_c$是两个集合都有的不相等整数的个数,N*t是两个集合一共有的不相等整数的个数。你的任务就是计算任意一对给定集合的相似度。

输入格式:

输入第一行给出一个正整数N(≤50),是集合的个数。随后N行,每行对应一个集合。每个集合首先给出一个正整数M(≤10^4^),是集合中元素的个数;然后跟M个[0,10^9^]区间内的整数。

之后一行给出一个正整数K(≤2000),随后K行,每行对应一对需要计算相似度的集合的编号(集合从1到N编号)。数字间以空格分隔。

输出格式:

对每一套方案,如果可行就输出YES,否则输出NO

输入样例:

输出样例:

50.00%
33.33%

思路

用set数组存储集合。对一对集合,先遍历其中一个集合,用find函数求出两个集合都有的不相等整数的个数,再求出两个集合一共有的不相等整数的个数。

或者用set_intersection()求交集

代码1

#include<iostream>
#include<set>
using namespace std;
const int N=1e2+5;
int main(){
set<int> s[N];
int n,m,k,x;
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&m);
for(int j=0;j<m;j++){
scanf("%d",&x);
s[i].insert(x);
}
}
scanf("%d",&k);
while(k--){
int a,b,c=0;
scanf("%d %d",&a,&b);
for(auto it:s[a]){
if(s[b].find(it)!=s[b].end()) c++;
}
double t=s[a].size()+s[b].size()-c;
printf("%.2lf%%\n",c/t*100);

}
return 0;
}
#include<iostream>
#include<set>
#include<vector>
#include <algorithm>
using namespace std;
const int N=1e2+5;
int main(){
set<int> s[N];
int n,m,k,x;
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&m);
for(int j=0;j<m;j++){
scanf("%d",&x);
s[i].insert(x);
}
}
scanf("%d",&k);
while(k--){
int a,b,c=0;
scanf("%d %d",&a,&b);
vector<int> res;
set_intersection(s[a].begin(),s[a].end(),s[b].begin(),s[b].end(),back_inserter(res));//求交集
c=res.size();
double t=s[a].size()+s[b].size()-c;
printf("%.2lf%%\n",c/t*100);

}
return 0;
}

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK