hdu4712 Hamming Distance

Hamming Distance

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

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
 

 

Recommend
liuyiding
用的预处理,随机算法,还是快了很多,主要用的原理也就是a^b=c,则c^b=a;这一点,就可以预处理,得出结果,我们可以看出最坏的复杂度,就是n*2^20,从理论上说,也不比n*n的复杂度,要好到哪里,但是,我们可以发现如果,n很大,那么结果就一定很小,这样,就可以得到最优结果后就退出了,别的什么bfs dfs,也是这种算法的变形,其次我们这题可以用随算法,枚举10w次就可以了,但个人认为不太好!
#include <iostream>
#include <stdio.h>
#include <vector>
#include <string.h>
using namespace std;
#define MAXN 100050
vector<int > vec[23];
int hash[1<<21],pri[MAXN];
char str[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
void init()
{
    int i;
    for(i=0;i<=21;i++)
    vec[i].clear();
    int ii=1<<21;
    for(i=0;i<ii;i++)
    {
        int ans=0;
        for(int j=0;j<21;j++)
        {
            if(i&(1<<j))ans++;
        }
        vec[ans].push_back(i);
    }
}
int main()
{
    init();
    int tcase,i,j,k,n;
    char str[20],c;
    scanf("%d",&tcase);
    while(tcase--)
    {
        memset(hash,0,sizeof(hash));
        scanf("%d",&n);
        bool flag=true;
        for(i=0;i<n;i++)
        {
            int ans=0;
            scanf("%X",&pri[i]);
           // printf("%d f\n",pri[i]);
            hash[pri[i]]++;
            if(flag&&hash[pri[i]]>=2)
            flag=false;

        }
        if(!flag)
        {
            printf("0\n");
            continue;
        }
        for(i=1;i<=20;i++)
        {
            for(j=0;j<n;j++)
            for(k=0;k<vec[i].size();k++)
            {
                if(hash[pri[j]^vec[i][k]])
                {
                    printf("%d\n",i);
                    goto my;
                }
            }
        }
        my:;

    }
    return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值