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. 学习blus老师js(1)--基础

    1.网页换肤: <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <t ...

  2. 32位汇编基础_cpu 寄存器

    32位通用寄存器 EAX EBX ECX EDX EBP ESP ESI EDI 八个寄存器都可以作为普通的数据寄存器使用.但有的有特殊的用途: EAX    为累加器, ECX    为计数器, E ...

  3. 学习笔记之Android

    Android 开发专区 - 开源中国社区 http://www.oschina.net/android 探索 Android Studio | Android Studio https://deve ...

  4. 使用libssh2连接到远程服务器

    libssh2-1.7.0.tar.gz  示例代码:libssh2-1.7.0.tar.gz\libssh2-1.7.0\example 官网示例 https://www.libssh2.org/e ...

  5. [置顶] linux c常用函数 (待完善)

    (1)字符测试函数 isalnum(测试字符是否为英文字母或数字) isalpha(测试字符是否为英文字母) isascii(测试字符是否为ASCII码字符) isblank(测试字符是否为空格字符) ...

  6. Unity容器声明周期管理

    Having said that, here is a solution that you can use with the Unity container: Create some custom a ...

  7. python manage.py runserver 0.0.0.0:8000

    python manage.py runserver 这种命令行,可以在服务器端输入IP:8000直接访问 在 python manage.py runserver 127.0.01:8000 在服务 ...

  8. C#使用UUID生成ID

    tring id = System.Guid.NewGuid().ToString(); 一句话即可,但此时id中有“-”符号存在,使用下面语句可变为纯字母+数字. string id = Syste ...

  9. Cloudera Manger CDH 安装文档

    简介: Cloudera Manager 是 Cloudera 公司推出的 Hadoop 集群管理工具,通过该管理工具可以方便的部署.配置.监控集群. Cloudera 公司自己发布的 Hadoop ...

  10. angular性能优化心得

    原文出处 脏数据检查 != 轮询检查更新 谈起angular的脏检查机制(dirty-checking), 常见的误解就是认为: ng是定时轮询去检查model是否变更.其实,ng只有在指定事件触发后 ...