zoukankan      html  css  js  c++  java
  • L2-005. 集合相似度

    按自己所学知识还有用自己思路写,样例什么都过,花费的时间太长了,好几秒才可以出答案,后台数据太大,没有方法的话肯定超时。 看大神代码才明白 set 原来这么好用,去重题太省时间了,下面是AC代码(set用法自己看着理解理解吧):

    #include<cstdio>
    #include<set>
    #include<cstdlib>
    using namespace std;
    const int maxn = 55;
    set<int> s[maxn];
    int n;
    int main()
    {
        while(~scanf("%d", &n))
        {
            int cnt;
            for(int i = 0; i < n; i++)
            {
                if(!s[i].empty())
                    s[i].clear();
                scanf("%d", &cnt);
                int x;
                for(int j = 0; j < cnt; j++)
                {
                    scanf("%d", &x);
                    s[i].insert(x);
                }
            }
            scanf("%d", &cnt);
            int u,v;
            for(int i = 0; i < cnt; i++)
            {
                scanf("%d%d", &u, &v);
                u--,v--;
                int same = 0;
                int size_s1 = s[u].size();
                int size_s2 = s[v].size();
                set<int>::iterator it;
                for(it = s[u].begin(); it != s[u].end(); it++)
                {
                    if(s[v].find(*it) != s[v].end())
                    {
                        same++;
                    }
                }
                int nc = same;
                int nt = size_s1+size_s2-same;
                double ans = (nc*1.0)/(nt*1.0)*100;
                printf("%.2lf%%
    ", ans);
            }
        }
        return 0;
    }
  • 相关阅读:
    揭秘富人的22种习惯与风格
    CSS
    浏览器--编辑器
    3 位运算 , 补码 ----在开发中比较少用
    2 Java数据类型+转义字符
    1 概述
    如何使用大脑
    JDBC
    web.xml文件的作用
    WindowBuilder插件探索
  • 原文地址:https://www.cnblogs.com/nr1999/p/8619244.html
Copyright © 2011-2022 走看看