Pat1063代码
题目描述:
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%
利用map和set的性质进行筛选,结果最后一个case超时。
代码:
#include<cstdio> #include<map> #include<set> #define N 55 using namespace std; set<int> s[N]; int main(int argc,char *argv[]) { int n; int i,j; scanf("%d",&n); for(i=1;i<=n;i++) { int m; int temp; scanf("%d",&m); for(j=1;j<=m;j++) { scanf("%d",&temp); s[i].insert(temp); } } int k; scanf("%d",&k); while(k--) { map<int,int> m; set<int>::iterator p; int x,y; scanf("%d%d",&x,&y); for(p=s[x].begin();p!=s[x].end();p++) { int index=*p; m[index]++; } for(p=s[y].begin();p!=s[y].end();p++) { int index=*p; m[index]++; } int count=0; map<int,int>::iterator it; for(it=m.begin();it!=m.end();it++) if(it->second>1) count++; printf("%.1f%%\n",100*count*1.0/m.size()); } return 0; }
AC代码:
利用函数求两个集合的交集,然后就可以求出集合的相似性了;
#include<cstdio> #include<set> #include<iterator> #include<algorithm> #define N 55 using namespace std; set<int> s[N]; int main(int argc,char *argv[]) { int n; int i,j; scanf("%d",&n); for(i=1;i<=n;i++) { int m; int temp; scanf("%d",&m); for(j=1;j<=m;j++) { scanf("%d",&temp); s[i].insert(temp); } } int k; scanf("%d",&k); while(k--) { int x,y; int ret,total; set<int> ans; scanf("%d%d",&x,&y); set_intersection(s[x].begin(),s[x].end(),s[y].begin(),s[y].end(), inserter(ans,ans.begin()));//求集合交集 ret=ans.size();//交集元素个数 total=s[x].size()+s[y].size()-ret;//不重复元素总个数 printf("%.1f%%\n",100*ret*1.0/total); } return 0; }
最后一个case跑了大概130ms;
AC代码:
写一个函数求集合的并集,其实这和上面的方法是一样的,只不过一个是交集,一个是并集;
#include<cstdio> #include<set> #include<iterator> #include<algorithm> #define N 55 using namespace std; set<int> s[N]; int GetUnion(int x,int y)//求并集元素的个数,其实algorithm里面有set_union { //这个函数,在这里我们可以简单的实现一下 int ret=0; set<int>::iterator it; for(it=s[x].begin();it!=s[x].end();it++) { if(s[y].find(*it)==s[y].end()) ret++; } ret+=s[y].size(); return ret; } int main(int argc,char *argv[]) { int n; int i,j; scanf("%d",&n); for(i=1;i<=n;i++) { int m; int temp; scanf("%d",&m); for(j=1;j<=m;j++) { scanf("%d",&temp); s[i].insert(temp); } } int k; scanf("%d",&k); while(k--) { int x,y; int ret,total; scanf("%d%d",&x,&y); total=GetUnion(x,y); ret=s[x].size()+s[y].size()-total; printf("%.1f%%\n",100*ret*1.0/total); } return 0; }
最后一个case跑了大概170ms;