Hamming DistanceTime
limit:6000/3000 MS (java/others) Memory limit:65535/65535 K (java/others)
Total submission (s): 1728 Accepted Submission (s): 680
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 Input
2212345543214123456789abcdef0137f
Sample Output
67 I want to say a word to the person who has the question, the problem of the pit father, the pit dad random algorithm may be submitted error, but more than a few times on the right (recommended that you should be 1e6 on it)#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include < cmath> #include <cstdio> #include <string> #include <ctime>using namespace std;typedef long long LL; const int MAXN = 1e5 + 5;const int INF = 0x3f3f3f3f;int T, n;int a[maxn];int get_num (int m) { int ret = 0; while (m) { ret + = (M & 1); M >>= 1; } return ret;} int main () { scanf ("%d", &t); while (T--) { scanf ("%d", &n); for (int i = 0; i < n; i + +) { scanf ("%x", &a[i]); } Srand (Time (NULL)); int Min = INF; for (int i = 0; i < 1e5; i + +) { int a = rand ()% n; int b = rand ()% n; if (a = = b) Continue; min = min (min, get_num (A[a] ^ a[b])); } printf ("%d\n", Min); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU-4712 Hamming Distance (algorithm of random numbers of pit fathers + brute force solution)