zoukankan      html  css  js  c++  java
  • PAT L2-005. 集合相似度 【stl set】

    给定两个整数集合,它们的相似度定义为:Nc/Nt*100%。其中Nc是两个集合都有的不相等整数的个数,Nt是两个集合一共有的不相等整数的个数。你的任务就是计算任意一对给定集合的相似度。

    输入格式:

    输入第一行给出一个正整数N(<=50),是集合的个数。随后N行,每行对应一个集合。每个集合首先给出一个正整数M(<=104),是集合中元素的个数;然后跟M个[0, 109]区间内的整数。

    之后一行给出一个正整数K(<=2000),随后K行,每行对应一对需要计算相似度的集合的编号(集合从1到N编号)。数字间以空格分隔。

    输出格式:

    对每一对需要计算的集合,在一行中输出它们的相似度,为保留小数点后2位的百分比数字。

    输入样例:

    3
    3 99 87 101
    4 87 101 5 87
    7 99 101 18 5 135 18 99
    2
    1 2
    1 3
    

    输出样例:

    50.00%
    33.33%
    

    这题我遇到了一个比较怪的地方。之前我用的是map存每组数是否存在,后来的搜索用map[a]查询,发现最后一组样例超时。但是用set.find(a)就不超时,不太明白为什么。
    百度说map和set都是红黑树实现的,效率应该是一样的。
    附ac代码:
     1 #include <cstdio>
     2 #include <cstring>
     3 #include <string>
     4 #include <iostream>
     5 #include <algorithm>
     6 #include <vector>
     7 #include <map>
     8 #include <queue>
     9 #include <map>
    10 #include <cmath>
    11 #include <set>
    12 using namespace std;
    13 const int maxn = 111;
    14 //map<int,map<int,int> >mp;
    15 set<int>st[maxn];
    16 set<int>::iterator it;
    17 
    18 int main()
    19 {
    20     int n;
    21     int a,m;
    22     scanf("%d",&n);
    23     for(int i=1;i<=n;++i)
    24     {
    25         scanf("%d",&m);
    26         for(int j=0;j<m;++j)
    27         {
    28             scanf("%d",&a);
    29             st[i].insert(a);
    30          //   mp[i][a]=1;
    31         }
    32     }
    33     int k;
    34     scanf("%d",&k);
    35     int b;
    36     for(int i=0;i<k;++i)
    37     {
    38 
    39         scanf("%d%d",&a,&b);
    40         int nk=st[a].size()+st[b].size(),nc=0;
    41         for( it=st[a].begin();it!=st[a].end();++it)
    42         {
    43             if(st[b].find(*it)!=st[b].end()) nc++; //if(mp[b][*it]==1) nc++;会超时
    44         }
    45         printf("%.2lf%%
    ",nc*1.0/(nk-nc)*100);
    46     }
    47     return 0;
    48 }
    View Code
  • 相关阅读:
    复杂对象创建终结者(Builder Pattern)
    创建型模式特立独行的两位大侠
    工厂模式(Factory)
    MAC 相关
    iOS 地图相关
    iOS字体相关
    App跳转系统设置界面
    Mac 模拟慢速网络
    Thread1:EXC_BAD_ACCESS 错误
    iOS 统计App 的代码总行数
  • 原文地址:https://www.cnblogs.com/zmin/p/8529880.html
Copyright © 2011-2022 走看看