标签:advance pat c++ 基础题 集合
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 3Sample Output:
50.0% 33.3%
#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; }
#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;
#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;
Pat(Advanced Level)Practice--1063(Set Similarity)
标签:advance pat c++ 基础题 集合
原文地址:http://blog.csdn.net/cstopcoder/article/details/24708099