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%

分析:两个集合共有的元素个数 / 两个集合合并后的元素个数.

有一组超时,待改进:

#include<iostream>
#include<set>
#include<vector>
using namespace std;
int main()
{
int N;
int i,count,j,t;
cin>>N;
vector< set<int> > vec(N);
for(i=0; i<N; i++){
scanf("%d",&count);
for(j=0; j<count; j++){
scanf("%d",&t);
vec[i].insert(t);
}
}
int request,a,b;
scanf("%d",&request);
for(i=0; i<request; i++){
scanf("%d %d",&a,&b);
set<int> temp_set;
set<int>::iterator it;
for(it = vec[a-1].begin(); it != vec[a-1].end(); it++){
temp_set.insert( *it );
}
for(it = vec[b-1].begin(); it != vec[b-1].end(); it++){
temp_set.insert( *it );
} printf( "%.1f",(float)( vec[a-1].size()+vec[b-1].size()-temp_set.size())*100/(float)(temp_set.size()) );
cout<<"%"<<endl;
}
return 0;
}

【PAT】1063. Set Similarity (25) 待改进的更多相关文章

  1. PAT 1063 Set Similarity (25)

    题意:给你n个集合,k次询问,每次询问求两个集合的(交集)/(并集). 思路:k有2000,集合大小有10000.先将每个集合排序,对每个询问分别设两个指针指向两个集合的头.设a[i]为指针1的值,b ...

  2. 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 ...

  3. 1063. Set Similarity (25)

    1063. Set Similarity (25) 时间限制 300 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given ...

  4. PAT 1063 Set Similarity[比较]

    1063 Set Similarity (25 分) Given two sets of integers, the similarity of the sets is defined to be N ...

  5. PAT 1063. Set Similarity

    1063. Set Similarity 题目大意 给定 n 个集合, k 个询问, 求任意两个集合的并集和合集. 思路 一道裸的考察 STL 中 set 的题, 我居然还用 hash 错过一遍, 用 ...

  6. PAT (Advanced Level) 1063. Set Similarity (25)

    读入之后先排序. 询问的时候可以o(m)效率得到答案. #include<cstdio> #include<cstring> #include<cmath> #in ...

  7. PAT甲题题解-1063. Set Similarity (25)-set的使用

    题意:两个整数集合,它们的相似度定义为:nc/nt*100%nc为两个集合都有的整数nt为两个集合一共有的整数注意这里的整数都是各不相同的,即重复的不考虑在内.给出n个整数集合,和k个询问,让你输出每 ...

  8. 【PAT甲级】1063 Set Similarity (25 分)

    题意: 输入一个正整数N表示集合的个数(<=50),接着输入N行,每行包括一个数字x代表集合的容量(<=10000),接着输入x个非负整数.输入一个正整数Q(<=2000),接着输入 ...

  9. 1063 Set Similarity (25分)

    Given two sets of integers, the similarity of the sets is defined to be /, where N​c​​ is the number ...

随机推荐

  1. 1021 docker常用命令和Jenkins搭建

    docker常用命令 1.镜像管理 docker pull (镜像名) # 默认拉取最新版本的镜像 docker pull (镜像名:版本号) #拉取指定版本的镜像 docker push (镜像名) ...

  2. bat文件

    bat文件是dos下的批处理文件.批处理文件是无格式的文本文件,它包含一条或多条命令.它的文件扩展名为 .bat 或 .cmd.在命令提示下键入批处理文件的名称,或者双击该批处理文件,系统就会调用cm ...

  3. Dynamics CRM 2011 权限管理

    CRM系统基于角色的权限主要通过部门.角色.用户.团队来进行控制.每一个实体记录的所有者(Owner)必然是某一个用户或团队.一个用户或团队必然归属于一个且只归属于一个部门,但团队的成员即用户可以来自 ...

  4. Sql Server 2016数据库生成带数据的脚本

    步骤:右键点击对应数据库->任务->生成脚本 在弹出的会话框中选择需要的对象,点击下一步,在设置和编写脚本选项中,点开高级按钮(如图)选择架构和数据点击确定就可以了. 提醒:如果你在数据库 ...

  5. OpenMP 简单的规约

    ▶ 简单的计算和规约 ● 第一种方法,将全局和的指针传入工作函数中进行加和,使用 critical 来控制临界区的访问 #include <stdio.h> #include <st ...

  6. javascript instanceof,typeof的区别

    区分string 与 String的区别 为什么结果会是false呢? <script type="text/javascript"> var aColors = [& ...

  7. DevExpress GridView 整理

    1:去除 GridView 头上的 "Drag a column header here to group by that column" -->  点击 Run Desig ...

  8. mac os 平台下载并编译android2.3.3源码

    现在在做有关android平台下的项目,最初对android环境各种不熟悉,搞了几个月终于有点眉目了,由于需要用到android本身提供的一些类似gps,tts等服务,单纯的看android提供的ja ...

  9. gradle 刷新缓存

    gradle build --refresh-dependencies -x test

  10. Apt-Cacher-ng 搭建缓存服务器

    docker pull minimum2scp/apt-cacher-ng docker run -d -p 3142:3142 --name=cache -e ACNG_BACKENDS_DEBIA ...