zoukankan      html  css  js  c++  java
  • hdu 4712 Hamming Distance 随机

    Hamming Distance

    Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)


    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
     

     思路:涨知识,还可以随机;

    #include<bits/stdc++.h>
    using namespace std;
    #define ll long long
    #define pi (4*atan(1.0))
    #define eps 1e-14
    const int N=1e5+10,M=4e6+10,inf=1e9+10,mod=1e9+7;
    const ll INF=1e18+10;
    int getnum(char a)
    {
        if(a>='0'&&a<='9')
            return a-'0';
        return a-'A'+10;
    }
    int check(int x,int y)
    {
        int sum=0;
        while(x||y)
        {
            if(x%2!=y%2)
            sum++;
            x>>=1;
            y>>=1;
        }
        return sum;
    }
    char ch[N][10];
    int mp[20][20];
    int main(){
        for(int i=0;i<16;i++)
        {
            for(int t=0;t<16;t++)
                mp[i][t]=check(i,t);
        }
        int T;
        scanf("%d",&T);
        while(T--)
        {
            int x;
            scanf("%d",&x);
            for(int i=0;i<x;i++)
            scanf("%s",ch[i]);
            int ans=inf;
            for(int i=0;i<=1000000;i++)
            {
                int k=0;
                int n=rand()%x;
                int m=rand()%x;
                if(n==m)continue;
                for(int t=0;t<5;t++)
                k+=mp[getnum(ch[n][t])][getnum(ch[m][t])];
                ans=min(ans,k);
            }
            printf("%d
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    《大型网站技术架构》学习笔记-03案例篇
    SpringBoot详细研究-05微服务与拾遗
    04证券市场典型违法违规行为及法律责任
    《大型网站技术架构》学习笔记-02架构篇
    DES & 3DES 加密算法
    技术的阐述能力
    python des ecb 加密 demo
    linux c 笔记-4 工程项目阅读推荐
    linux c 笔记-3 c语言基础知识
    linux c 笔记-2 Hello World & main函数
  • 原文地址:https://www.cnblogs.com/jhz033/p/5931903.html
Copyright © 2011-2022 走看看