HDU 4712:Hamming Distance

Hamming Distance

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


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
 

迷失在幽谷中的鸟儿,独自飞翔在这偌大的天地间,却不知自己该飞往何方……


题意:给出n个字符串,求每两个串相应位异或值的二进制中1的个数和的最小值!
例如:
12345
54321

其相应位异或之后为:46064
它的每一位二进制和为6


一般的枚举会超时,因为我试过……,然后为了优化,给两个数字异或之后的值打表!然后,然后还是超时……
既然不能遍历每一种情况,那就随机吧!只要控制随机总次数,就可以控制时间长度,至于AC,那就靠运气啦!不过这样的随机总次数,一般都会AC……

#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <time.h>
using namespace std;
int  cmp[16][16]= {0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,1,0,2,1,2,1,3,2,2,1,3,2,3,2,4,3,1,2,0,1,2,3,1,2,2,3,1,2,3,4,2,3,2,1,1,0,3,2,2,1,3,2,2,1,4,3,3,2,1,2,2,3,0,1,1,2,2,3,3,4,1,2,2,3,2,1,3,2,1,0,2,1,3,2,4,3,2,1,3,2,2,3,1,2,1,2,0,1,3,4,2,3,2,3,1,2,3,2,2,1,2,1,1,0,4,3,3,2,3,2,2,1,1,2,2,3,2,3,3,4,0,1,1,2,1,2,2,3,2,1,3,2,3,2,4,3,1,0,2,1,2,1,3,2,2,3,1,2,3,4,2,3,1,2,0,1,2,3,1,2,3,2,2,1,4,3,3,2,2,1,1,0,3,2,2,1,2,3,3,4,1,2,2,3,1,2,2,3,0,1,1,2,3,2,4,3,2,1,3,2,2,1,3,2,1,0,2,1,3,4,2,3,2,3,1,2,2,3,1,2,1,2,0,1,4,3,3,2,3,2,2,1,3,2,2,1,2,1,1,0};;
char p[1000005][10];
int ddd(int q,int w)
{
    int a,b,s=0;
    for(int i=0; i<5; i++)
    {
        char x = p[q][i];
        char y = p[w][i];
        if(isdigit(x))a=x-'0';
        else a=x-'A'+10;
        if(isdigit(y))b=y-'0';
        else b=y-'A'+10;
        s += cmp[a][b];
    }
    return s;
}
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        int N;
        cin>>N;
        int minn=0xfffffff;
        for(int i=0; i<N; i++)
            cin>>p[i];
        srand((unsigned)time(NULL));
        for(int i=1; i<=100000; i++)
        {
            int a=rand()%N;
            int b=rand()%N;
            if(a==b)continue;
            int tmp=ddd(a,b);
            minn=tmp>minn?minn:tmp;
        }
        cout<<minn<<endl;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值