zoukankan      html  css  js  c++  java
  • 1063 Set Similarity (25)

    Given two sets of integers, the similarity of the sets is defined to be N~c~/N~t~*100%, where N~c~ is the number of distinct common numbers shared by the two sets, and N~t~ 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 (<=10^4^) and followed by M integers in the range [0, 10^9^]. 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%

    这种方法有一个测试点不能通过,原因是建立map的开销比较大,并且再查询的时候,会反复的建立map,是一个很耗费时间的过程,应该在输出数据的时候就为每一组数据建立map,这样能避免之后重复建立map;
    在编程中应该避免重复没有必要的操作
     1 #include<iostream>
     2 #include<vector>
     3 #include<map>
     4 #include<set>
     5 using namespace std;
     6 int main(){
     7   int n, m, i, j;
     8   vector<int> v[51];
     9   cin>>n;
    10   for(i=0; i<n; i++){
    11     cin>>m;
    12     for( j=0; j<m; j++){
    13       int temp;
    14       scanf("%d", &temp);
    15       v[i].push_back(temp);
    16     }
    17   }
    18   cin>>m;
    19   for(i=0; i<m; i++){
    20     int a, b, cnt=0, sum=0;
    21     cin>>a>>b;
    22     map<int, int> mmap;
    23     for(j=0; j<v[a-1].size(); j++)
    24         if(mmap.count(v[a-1][j])==0) mmap[v[a-1][j]] = 1;
    25     for(j=0; j<v[b-1].size(); j++){
    26         if(mmap.count(v[b-1][j])==0) mmap[v[b-1][j]] = 2;
    27         else if(mmap[v[b-1][j]]==1){
    28             cnt++;
    29             mmap[v[b-1][j]]=2;
    30         }
    31     }
    32     printf("%.1f%%
    ", cnt*100.0/mmap.size());
    33   }
    34   return 0;
    35 }

    修改:在输入数据的时候,就组织数据, 并用更简洁的set来建立数据结构

     1 #include<iostream>
     2 #include<set>
     3 #include<vector>
     4 using namespace std;
     5 int main(){
     6   int n, m, i,j;
     7   cin>>n;
     8   vector<set<int>> v;
     9   for(i=0; i<n; i++){
    10     set<int> s;
    11     cin>>m;
    12     int temp;
    13     for(j=0; j<m; j++){
    14       cin>>temp;
    15       s.insert(temp);
    16     }
    17     v.push_back(s);
    18   }
    19   cin>>m;
    20   for(i=0; i<m; i++){
    21     int a, b;
    22     cin>>a>>b;
    23     int nc=v[a-1].size(), nt=0;
    24     for(auto it=v[b-1].begin(); it!=v[b-1].end(); it++){
    25       if(v[a-1].count(*it)!=0) nt++;
    26       else nc++;
    27     }
    28     printf("%.1f%%
    ", 100.0*nt/nc);
    29   }
    30   return 0;
    31 }
    有疑惑或者更好的解决方法的朋友,可以联系我,大家一起探讨。qq:1546431565
  • 相关阅读:
    QT 5 安装 vs2017 后,出现找不到 rc.exe 问题
    git push -f
    使用druid连接池的超时回收机制排查连接泄露问题
    git 记住密码
    postgresql c library use
    jvm内存溢出分析
    Maven中使用本地JAR包
    执行Git命令时出现各种 SSL certificate problem 的解决办法
    transformer模型计算图
    jieba分词单例模式及linux权限不够情况下tmp_dir自定义
  • 原文地址:https://www.cnblogs.com/mr-stn/p/9156346.html
Copyright © 2011-2022 走看看