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 are equal to the number of ones in a XO R B. for calculating Hamming distance between, strings A and B, they must has equal length.
Now given N different binary strings, please calculate the minimum Hamming distance between every pair of strings.
Inputthe first line of the input was an integer T, the number of test cases. (0<T<=20) Then T-test case followed. The first line of all test case was 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".
Outputfor each test case, output the minimum Hamming distance between every pair of strings.
Sample input2212345543214123456789abcdef0137f
Sample Output67 The normal practice of this problem is O (n^2), it is obvious that this practice will be very big t off but we can use randomization, and then quietly not talking about the water past =
#include <iostream>#include<stdio.h>#include<algorithm>#include<string.h>#include<time.h>using namespacestd;#defineN 100000Charstr[n+Ten][Ten];intmark[ -][ -];//The number of 1 of i^j saved in makeintarr[]={0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4};//number of 1 in 0-fintChartohex (CharCh//convert 0-f characters to 10 binary calculation{ if(IsDigit (CH))returnch-'0'; returnch-'A'+Ten;}voidGetmark ()//find the Mark array{ inti,j,s; for(i=0;i< -; i++) { for(j=i;j< -; j + +) {s=i^J; MARK[I][J]=mark[j][i]=Arr[s]; } }}intGeths (intXintY//Hamming distance for x to y{ inti,sum=0; for(i=0;i<5; i++) { intxx =Chartohex (Str[x][i]); intyy =Chartohex (Str[y][i]); Sum+=Mark[xx][yy]; } returnsum;}intMain () {intT; Getmark (); scanf ("%d",&t); while(t--) { intN; scanf ("%d",&N); inti; for(i=0; i<n;i++) {scanf ("%s", Str[i]); } Srand (Time (NULL)); intx,y,mins= -; for(i=0;i<900000; i++)//random 900,000 times basic can, in the premise of not timeout, the more random times the better{x=rand ()%N; Y=rand ()%N; if(x==y)Continue; inttemp =geths (x, y); if(mins>temp) mins=temp; } printf ("%d\n", mins); } return 0;}/*2212345543214123456789abcdef0137f*/
HDU 4217 Hamming Distance randomization of water past