1063. Set Similarity (25)

来源:互联网 发布:淘宝无线客服链接生成 编辑:程序博客网 时间:2024/05/07 20:03

1063. Set Similarity (25)

时间限制
300 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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:
33 99 87 1014 87 101 5 877 99 101 18 5 135 18 9921 21 3
Sample Output:
50.0%33.3%
总共N个集合;
每个集合的【初始】元素个数(有重复)   集合元素;【Ps:集合编号1~N,这里输入的时候要注意去除重复,我用set.inset(),会自动去重】
……
接下来K行
集合a 集合b 
……
输出K行,每对集合的    交集元素个数(相同原数个数)  占  并集元素个数(两个集合元素合在一起去重复以后的元素个数)  的百分比
下面两个代码,一个ac另一个最后一个测试点超时

评测结果

时间结果得分题目语言用时(ms)内存(kB)用户8月13日 10:36答案正确251063C++ (g++ 4.7.2)1472744datrilla

测试点

测试点结果用时(ms)内存(kB)得分/满分0答案正确118012/121答案正确13083/32答案正确11803/33答案正确64363/34答案正确14727444/4
#include <iostream> #include <vector>#include<set> using namespace std;void readlnsets(vector<set<int>>*sets, int N){  int M, integers,index;  for (index = 0; index < N; index++)  {    scanf("%d", &M);    while (M--)    {      scanf("%d", &integers);      (*sets)[index].insert(integers);/*当数已经在集合里面就不再放入了*/    }   }}int main(){  int N,K,a,b;  double Nt, Nc;   scanf("%d", &N);  vector<set<int>>sets(N);  readlnsets(&sets,N);  scanf("%d", &K);  while (K--)  {    scanf("%d%d", &a, &b);    a--;     b--;    Nc = 0.0;    for (set<int>::iterator iter = sets[a].begin(); iter != sets[a].end(); iter++)      if (sets[b].find((*iter))!= sets[b].end())Nc++;     Nt = sets[a].size() + sets[b].size()-Nc;     printf("%.1lf%\n", Nc / Nt*100.0);     }  system("pause");  return 0;}


评测结果

时间结果得分题目语言用时(ms)内存(kB)用户8月13日 10:25部分正确211063C++ (g++ 4.7.2)19436datrilla

测试点

测试点结果用时(ms)内存(kB)得分/满分0答案正确138412/121答案正确13083/32答案正确11803/33答案正确194363/34运行超时  0/4
#include <iostream> #include <vector>#include<set>#include<iomanip>using namespace std;void readlnsets(vector<set<int>>*sets, int N){  int M, integers,index;  for (index = 0; index < N; index++)  {    cin >> M;    while (M--)    {      cin >> integers;      (*sets)[index].insert(integers);/*当数已经在集合里面就不再放入了*/    }   }}int main(){  int N,K,a,b;  double Nt, Nc;  set<int>Uset;  cin >> N;  vector<set<int>>sets(N);  readlnsets(&sets,N);  cin >> K;  while (K--)  {    cin >> a >> b;    a--;     b--;    Uset.insert(sets[a].begin(),sets[a].end());    Uset.insert(sets[b].begin(), sets[b].end());    Nt = Uset.size();    Nc = sets[a].size() + sets[b].size() - Nt;      cout << setiosflags(ios::fixed) << setprecision(1) << Nc / Nt *100.0<<"%"<< endl;    Uset.clear();  }  system("pause");  return 0;}

0 0
原创粉丝点击