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;
    }
  • 相关阅读:
    Shell 函数
    Shell 流程控制
    Shell test 命令
    Shell echo命令
    python 类、模块、包的区别
    postgresql vacuum table
    ssh连接断开后 shell进程退出
    ubuntu 搭建 svn服务器,使用http方式访问
    如何查看apache加载了哪些模块
    maven 的使用
  • 原文地址:https://www.cnblogs.com/nr1999/p/8619244.html
Copyright © 2011-2022 走看看