zoukankan      html  css  js  c++  java
  • HDU 4712Hamming Distance(随机函数运用)

    Hamming Distance

    Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
    Total Submission(s): 1043    Accepted Submission(s): 394

    Problem Description
    (From wikipedia) For binary strings a and b the Hamming distance is equal to the number of ones in a XOR b. For calculating Hamming distance between two strings a and b, they must have equal length.
    Now given N different binary strings, please calculate the minimum Hamming distance between every pair of strings.
     
    Input
    The first line of the input is an integer T, the number of test cases.(0<T<=20) Then T test case followed. The first line of each test case is an integer N (2<=N<=100000), the number of different binary strings. Then N lines followed, each of the next N line is a string consist of five characters. Each character is '0'-'9' or 'A'-'F', it represents the hexadecimal code of the binary string. For example, the hexadecimal code "12345" represents binary string "00010010001101000101".
     
    Output
    For each test case, output the minimum Hamming distance between every pair of strings.
     
    Sample Input
    2 2 12345 54321 4 12345 6789A BCDEF 0137F
     
    Sample Output
    6 7
     
    Source
     


          题目大意:给你很多串,最多10^5个串,每个串长度是5,16进制转化为2进制,问你任意两个串抑或得到1的最小的个数。

    解题思路: 做题的时候只顾着如何降低复杂度,简单的O(n^2)肯定会超时,最后才得知是随机函数写。结果只可能是0~20.自己写的时候10W次还是WA了,人品不行啊,果断随机100W次A了。 不过听说可以更暴力地直接枚举最前面的10000个也可以A。网络赛要敢于尝试。

    题目地址:Hamming Distance

    AC代码:
    #include<iostream>
    #include<cstring>
    #include<string>
    #include<cmath>
    #include<cstdio>
    #include<algorithm>
    using namespace std;
    int a[100005];
    
    int main()
    {
        int tes,i,j,k,res,ans;
        scanf("%d",&tes);
        while(tes--)
        {
            int n;
            scanf("%d",&n);
            for(i=0;i<n;i++)
                scanf("%X",&a[i]);  //16进制读取
                
            res=20;  //结果初始为最大20
            for(i=1;i<=1000000;i++)
            {
                j=rand()%n;  //随机函数
                k=rand()%n;
                if(j==k)
                    continue;
                ans=0;
                int tmp=a[j]^a[k];  //抑或
                while(tmp)  //抑或算1的个数,保存到ans中
                {
                    if(tmp&1)
                        ans++;
                    tmp>>=1;
                }
                if(ans<res)
                    res=ans;
            }
            cout<<res<<endl;
        }
        return 0;
    }
    
    //2453MS 676K
    




  • 相关阅读:
    centos6安装mono
    Flashcache系统管理员手册
    【ZT】超乎想象 HTML5九大超酷特效体验
    程序员不可不知的C#代码规范
    【推薦】帮你炼成软件架构师的97件事
    [ZT]智能客户端(Smart Client)
    【ZT】成就大型高性能网站的十项规则
    通過SQL取出所有周六/周日的日期到Table
    【杯具】面试才说一句话就被轰出来了
    Integer在webservice的传递
  • 原文地址:https://www.cnblogs.com/pangblog/p/3315560.html
Copyright © 2011-2022 走看看