HDU 4712 Hamming Distance (random function)

Source: Internet
Author: User

Hamming Distance

Time limit:6000/3000 MS (java/others) Memory limit:65535/65535 K (java/others)
Total submission (s): 1806 Accepted Submission (s): 714


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&LT;T&LT;=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

Source2013 ACM/ICPC Asia Regional online--warmup test instructions: n hexadecimal number, convert two hexadecimal number to binary string after bitwise XOR, the last obtained binary string contains 1 of the minimum number. Analysis: In fact, this thought for a long time is not know how to optimize, because the data also has 10W, but finally unexpectedly told me is to use random numbers, think of the most also on 20 strings, each time randomly selected two, the probability of getting the correct answer is still very large, but not 100%, The greater the number of random numbers that should be taken, the more accurate the answer.
#pragmaCOMPRINT (linker, "/stack:1024000000,1024000000")#include<cstdio>#include<string>#include<iostream>#include<cstring>#include<cmath>#include<stack>#include<queue>#include<vector>#include<map>#include<stdlib.h>#include<time.h>#include<algorithm>#defineLL __int64#defineFIN freopen ("In.txt", "R", stdin)using namespacestd;Const intmaxn=100000+5;Const intmax=1<< +;Const intinf=0x3f3f3f3f;inta[max+5],NUM[MAXN];intKase,n;voidinit () { for(intI=0; i<=max;i++)    {        intCnt=0;  for(intj=0;j< +; j + +)            if(I & (1<<j)) CNT++; A[i]=CNT; }}intMain () {init (); scanf ("%d",&Kase);  while(kase--) {scanf ("%d",&N);  for(intI=0; i<n;i++) scanf ("%x",&Num[i]); intans=INF;        Srand ((unsigned) time (NULL));  for(intI=0;i<900000; i++)        {            intX=rand ()%N; intY=rand ()%N; if(x==y)Continue; Ans=min (ans,a[num[x]^Num[y]]); } printf ("%d\n", ans); }    return 0;}
View Code

HDU 4712 Hamming Distance (random function)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.