HDU 4712 Hamming Distance

38 篇文章 0 订阅
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

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

bitset+随机化~

随机化真是神奇……

输入16进制直接用%x,不用输入字符串然后再算。

每次随机化两个点,然后放在bitset里面计算hamming distance,更新答案即可~


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<climits>
#include<bitset>
using namespace std;
#define inf INT_MAX

int t,n,x,y,a[100000],cnt,ans,num;
bitset<30> c;

int main()
{
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);cnt=1000000;ans=inf;
		for(int i=0;i<n;i++) scanf("%x",&a[i]);
		while(cnt--)
		{
			x=rand()%n;y=rand()%n;
			while(x==y) y=rand()%n;
			c=a[x]^a[y];num=c.count();
			ans=min(ans,num);
		}
		printf("%d\n",ans);
	}
	return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值