zoukankan      html  css  js  c++  java
  • HDU 4217 Hamming Distance 随机化水过去

    Hamming Distance

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


    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
     
    这道题的正常做法是O(n^2)的,很显然这个做法会大大方方的T掉
    但是我们可以用随机化,然后悄悄不说话的水过去= =
    #include <iostream>
    #include <stdio.h>
    #include <algorithm>
    #include <string.h>
    #include <time.h>
    
    using namespace std;
    #define N 100000
    
    char str[N+10][10];
    int mark[20][20];  //make中存 i^j 的1的个数
    int arr[]={0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4};  //0-F 中1的个数
    
    int charToHex(char ch)  //将0-F字符转换成10进制数计算
    {
        if(isdigit(ch)) return ch-'0';
        return ch-'A'+10;
    }
    
    void getMark() //求mark数组
    {
        int i,j,s;
        for(i=0;i<16;i++)
        {
            for(j=i;j<16;j++)
            {
                s=i^j;
                mark[i][j]=mark[j][i]=arr[s];
            }
        }
    }
    
    int geths(int x,int y) //求x到y的Hamming distance
    {
        int i,sum=0;
        for(i=0;i<5;i++)
        {
            int xx = charToHex(str[x][i]);
            int yy = charToHex(str[y][i]);
            sum+=mark[xx][yy];
        }
        return sum;
    }
    
    int main()
    {
        int t;
        getMark();
        scanf("%d",&t);
        while(t--)
        {
            int n;
            scanf("%d",&n);
            int i;
            for(i=0;i<n;i++)
            {
                scanf("%s",str[i]);
            }
            srand(time(NULL));
            int x,y,mins=100;
            for(i=0;i<900000;i++) //随机900000次基本能过,在不超时的前提下,随机次数越多越好
            {
                x=rand()%n;
                y=rand()%n;
                if(x==y)    continue;
                int temp = geths(x,y);
                if(mins>temp)   mins=temp;
            }
            printf("%d
    ",mins);
        }
        return 0;
    }
    /*
    2
    2
    12345
    54321
    4
    12345
    6789A
    BCDEF
    0137F
    */
  • 相关阅读:
    关于程序员的段子,你能读懂几个?网友:你若全对算我输!
    【C++学习笔记】超详细C++注释的使用方法,不赶紧收藏就错过啦!
    做编程容易短命?衷心建议:三十六计,“保命”要紧
    【C++学习笔记】C++异常处理!你绝对不能错过的干货!
    Linux Socket套接字出现问题怎么办?教你5个方法“有备无患”
    【致敬伟大的程序员】写代码写进国博,这么牛的还有谁?
    重要的事情说三遍:局部变量一定要初始化!你做到了吗?
    自我介绍
    .NET应用程序中嵌入VB6表单
    在VB6中使用。net DLL
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4147275.html
Copyright © 2011-2022 走看看