1063. Set Similarity (25)
2013-09-28 14:08
267 查看
1063. Set Similarity (25)
时间限制300 ms内存限制32000 kB代码长度限制16000 B判题程序Standard作者CHEN, YueGiven 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 separatedby 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%
解题思路:
输入数组后,根据输入的数组序号对数组进行排序,然后去重,再进行类似于merge操作的遍历,获得两个数组中不同的数的个数。
#include <iostream>#include"stdio.h"#include"stdlib.h"#include"algorithm"using namespace std;struct store{int a[10000];int length;bool sort_flag;//标记数组是否已经排序,可以避免重复排序int cnt;};int main(){int N,M,K;struct store s[50];scanf("%d",&N);for(int i=0;i<N;i++){scanf("%d",&M);s[i].length = M;s[i].cnt=M;s[i].sort_flag = false;for(int j=0;j<M;j++){scanf("%ld",&s[i].a[j]);}}scanf("%d",&K);int first,second;for(int k=0;k<K;k++){scanf("%d%d",&first,&second);first--;second--;if(!s[first].sort_flag){sort(s[first].a,s[first].a+s[first].length);//快排
//去重for(int i=s[first].length-1;i>=1;i--){if(s[first].a[i]==s[first].a[i-1]){s[first].a[i]=-1;s[first].cnt--;//记下数组中distinct的数的个数}}s[first].sort_flag=true;//已经排序了}if(!s[second].sort_flag){sort(s[second].a,s[second].a+s[second].length);for(int i=s[second].length-1;i>=1;i--){if(s[second].a[i]==s[second].a[i-1]){s[second].a[i]=-1;s[second].cnt--;}}s[second].sort_flag=true;}int distinct=0;int i=0,j=0;while(i<s[first].length&&j<s[second].length){if(s[first].a[i]!=-1&&s[second].a[j]!=-1){if(s[first].a[i]<s[second].a[j]){distinct++;i++;}else if(s[first].a[i]>s[second].a[j]){distinct++;j++;}else{i++;j++;}}else{if(s[first].a[i]==-1&&s[second].a[j]!=-1){i++;}else if(s[first].a[i]!=-1&&s[second].a[j]==-1){j++;}else /*if(s[first].a[i]!=-1&&s[second].a[j]!=-1)*/{//用else if会超时i++;j++;}}}while(i<s[first].length){if(s[first].a[i]!=-1)distinct++;i++;}while(j<s[second].length){if(s[second].a[j]!=-1)distinct++;j++;}long common = (s[first].cnt+s[second].cnt-distinct)/2;printf("%.1f%\n",(common*100.0/(common+distinct)));}return 0;}
相关文章推荐
- PAT (Advanced Level) 1063. Set Similarity (25)
- PAT 1063. Set Similarity (25) 寻找交集。未解之谜。
- 1063. Set Similarity (25)
- PAT - 甲级 - 1063. Set Similarity (25)(STL-set)
- 1063. Set Similarity (25)
- 1063. Set Similarity (25)
- 浙江大学PAT_甲级_1063. Set Similarity (25)
- 1063. Set Similarity (25)
- 1063. Set Similarity (25)遍历map
- 【PAT】【Advanced Level】1063. Set Similarity (25)
- 1063. Set Similarity (25)
- 1063. Set Similarity (25)【集合操作】——PAT (Advanced Level) Practise
- 1063. Set Similarity (25)
- 1063. Set Similarity (25)
- Python实现PAT 1063. Set Similarity (25)
- PAT 1063. Set Similarity (25)
- PAT (Advanced Level) 1063. Set Similarity (25) set的相似度
- 1063. Set Similarity (25)
- 1063. Set Similarity (25)
- PAT (Advanced Level) Practise 1063 Set Similarity (25)