hdu4712Hamming Distance(随机大法好)

这道题的题意是有N个点,每个点都是由五位的十六进制数组成的,每个点之间的距离就是他们转化成二进制之后的亦或和为1的个数,问你这这些点中距离最短是多少。

虽然题目给出的点很多,但是因为他只有5位,所以一共就 5^16个。这样的话只要随机两个点看一下最小距离就好了。

AC代码:

/* ***********************************************
Author        :yzkAccepted
Created Time  :2016/5/15 11:22:31
TASK		  :ggfly.cpp
LANG          :C++
************************************************ */

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <stack>
using namespace std;
typedef long long ll;
const int maxn=100010;
const int inf=1<<27;
int a[maxn];
int get(int val)
{
	int ans=0;
	while(val>0)
	{
		ans+=val%2;
		val/=2;
	}
	return ans;
}
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int casn,i,j;
	cin>>casn;
	for(i=1;i<=casn;i++)
	{
		int n;
		scanf("%d",&n);
		for(int j=0;j<n;j++)
			scanf("%X",&a[j]);
		int ans=inf;
		for(int j=1;j<=1000000;j++)
		{
			int idx1=rand()%n;
			int idx2=rand()%n;
			while(idx1==idx2) idx2=rand()%n;
			int val=a[idx1]^a[idx2];
			ans=min(ans,__builtin_popcountl(val));
		}
		cout<<ans<<endl;
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值