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. 清理Visual Studio中VC++工程里不需要的文件

    Visual Studio开发C++,工程的空间几M,几十M甚至几百M的长,生成的中间文件看的眼花缭乱,占空间不说,特别是备份拷贝代码时无奈的等待,有了这个脚本,好吧,整个世界清静了. @echo o ...

  2. unittest框架出报告乱码的问题解决

    跟着上面的步骤修改好后,unittest断言写法要写成下面这样才能展示非乱码

  3. Java 获取字符串指定下标位置的值 charAt()

    Java手册 charAt public char charAt(int index) 返回指定索引处的 char 值.索引范围为从 0 到 length() - 1.序列的第一个 char 值位于索 ...

  4. 汇编_指令_IRET

    IRET(interrupt return)中断返回,中断服务程序的最后一条指令.   汇编指令IRET [指令格式]IRET   [指令功能]IRET(interrupt return)中断返回,中 ...

  5. 超简单的制作win7 U盘启动

    我感觉真的太简单,操作so简单 第一个下载这个工具,这是微软官方提供的,用这个工具可以把win7的iso文件刻录到u盘中,u盘就可以作为系统启动盘来使用了 Windows 7 USB DVD Down ...

  6. 深入理解基于selenium的二次开发

    对于做web端自动化测试的人来说,可能接触selenium比QTP还要多,但是我们在做基于selenium的二次开发的时候,经常会说到二次开发是为了易于维护,很多人可能不懂得维护的价值是什么,和到底要 ...

  7. django-中间件,流量统计实例

    Django中间件(Middleware) 中间件,顾名思义,就是处在中间的一些软件.比如匹配到了URL,但是还没有执行view函数的时候,这个时候可以执行一些代码,这个代码就是中间件. HttpRe ...

  8. eclipse liquibase 插件

    http://marketplace.eclipse.org/category/free-tagging/liquibase http://marketplace.eclipse.org/market ...

  9. php遍历所有文件

    <?php function getdir($dir){ static $str = ''; if(is_file($dir)){ $str.=$dir.'<br>'; }else{ ...

  10. zookeeper简绍

    先一句话概括下zookeeper:zookeeper可谓是目前使用最广泛的分布式组件了.其功能和职责单一,但却非常重要. zookeeper到底是什么?(技术文) 1)zookeeper实际上是yah ...