1063. Set Similarity (25)

102 篇文章 0 订阅
37 篇文章 0 订阅

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:
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%
总共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/12
1答案正确13083/3
2答案正确11803/3
3答案正确64363/3
4答案正确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/12
1答案正确13083/3
2答案正确11803/3
3答案正确194363/3
4运行超时  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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值