1063. Set Similarity (25)
1063. Set Similarity (25)
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%
解题思路:
输入数组后,根据输入的数组序号对数组进行排序,然后去重,再进行类似于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;
}
1063. Set Similarity (25)的更多相关文章
- PAT 甲级 1063 Set Similarity (25 分) (新学,set的使用,printf 输出%,要%%)
1063 Set Similarity (25 分) Given two sets of integers, the similarity of the sets is defined to be ...
- 【PAT】1063. Set Similarity (25) 待改进
Given two sets of integers, the similarity of the sets is defined to be Nc/Nt*100%, where Nc is the ...
- 1063 Set Similarity (25分)
Given two sets of integers, the similarity of the sets is defined to be /, where Nc is the number ...
- PAT 1063 Set Similarity (25)
题意:给你n个集合,k次询问,每次询问求两个集合的(交集)/(并集). 思路:k有2000,集合大小有10000.先将每个集合排序,对每个询问分别设两个指针指向两个集合的头.设a[i]为指针1的值,b ...
- PAT (Advanced Level) 1063. Set Similarity (25)
读入之后先排序. 询问的时候可以o(m)效率得到答案. #include<cstdio> #include<cstring> #include<cmath> #in ...
- PAT甲题题解-1063. Set Similarity (25)-set的使用
题意:两个整数集合,它们的相似度定义为:nc/nt*100%nc为两个集合都有的整数nt为两个集合一共有的整数注意这里的整数都是各不相同的,即重复的不考虑在内.给出n个整数集合,和k个询问,让你输出每 ...
- 【PAT甲级】1063 Set Similarity (25 分)
题意: 输入一个正整数N表示集合的个数(<=50),接着输入N行,每行包括一个数字x代表集合的容量(<=10000),接着输入x个非负整数.输入一个正整数Q(<=2000),接着输入 ...
- PAT 1063 Set Similarity[比较]
1063 Set Similarity (25 分) Given two sets of integers, the similarity of the sets is defined to be N ...
- PAT 1063. Set Similarity
1063. Set Similarity 题目大意 给定 n 个集合, k 个询问, 求任意两个集合的并集和合集. 思路 一道裸的考察 STL 中 set 的题, 我居然还用 hash 错过一遍, 用 ...
随机推荐
- ASP.NET 5探险(1):Azure中配置连接字符串、独立项目执行EF7数据迁移
(此文章同时发表在本人微信公众号“dotNET每日精华文章”,欢迎右边二维码来关注.) 题记:我开始把ASP.NET 5用于生产系统开发已经有1个多月了,也填了一些坑积累了一些经验,从今天开始会陆陆续 ...
- 自己动手写RTP服务器——关于RTP协议
转自:http://blog.csdn.net/baby313/article/details/7353605 本文会带领着你一步步动手实现一个简单的RTP传输服务器,旨在了解RTP流媒体传输协议以及 ...
- ssh 免密码登陆
远程ssh登陆服务器或者其他机器时或者scp时,需要输入密码,感觉很麻烦,于是研究如何免密码登陆. step1:Client端生成公钥和密钥 执行命令 ssh-keygen 进入目录~/.ssh里面, ...
- 面向服务的架构SOA
SOA简介 SCA实现SOA的最佳方式 Apache开源框架Tuscany实现SCA架构 SOA简单描述: SOA(Service-Oriented Architecture)面向服务的体系架构.为了 ...
- 【JSON 注解】JSON循环引用1-----Jackson常用注解介绍 eq:@JsonIgnore
循环引用:实体A与实体B有关系,A中有B作为字段,B中有A作为一个字段.查询A对象后,将A对象转化为JSON格式数据时,会因为序列化过程中导致A中有B字段,B字段中又有A,这样就引起了循环引用的问题! ...
- RecyclerView再封装
RecyclerView做为ListView的替代品,已经出了很久了,既然是替代品,那自然有些ListView没有的优点.比如说:可以随意切换list,grid,stagger.可以指定一个或多个it ...
- 运用Python成为黑客
1.Fuzz测试: 漏洞挖掘有三种方法:白盒代码审计.灰盒逆向工程.黑盒测试.其中黑盒的Fuzz测试是效率最高的一种,能够快速验证大量潜在的安全威胁. Fuzz测试,也叫做"模糊测试&quo ...
- 09_IO流
1. IO(Input Output)流 IO流用来处理设备之间的数据传输 Java对数据的操作时通过流的方式 Java用于操作流的对象都在IO包中 流按操作数据分为两种: 字节流和字符流 流按类型分 ...
- hdu 5459 Jesus Is Here (费波纳茨递推)
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 65535/102400 K (Java/Others)Total Submission ...
- hdfs 集群间拷贝
hadoop distcp -i hdfs://192.168.10.211:9000/fileinfo hdfs://192.168.24.46:9000/fileinfo distcp [OPTI ...