hdu 4712 Hamming Distance

Problem

acm.hdu.edu.cn/showproblem.php?pid=4712

Reference

多向 bfs 思路
CSDN-markdown 用 LaTeX

Meaning

定义两个整数数 a 和 b 的汉明距离为: ab 的二进制表示中 1 的个数。
给出 n 个 5 位十六进制表示的数,求所有数对的汉明距离中的最小值。

Analysis

暴力会超时。
多次随机选两个不同的数来算海明距离,并更新答案。
试算下概率…
记从 n 个数中任选两个,刚好选到那两个能产生最小海明距离的数(假设只有一对这样的数)的概率是 p,
p=1C2n
而选不中的概率就是 1p
随机 m 次的话,都选不中的概率就是 (1p)m ,m很大失败概率就会变得很小,而成功概率 1(1p)m 就会接近 1。
另外还有多向 bfs 的思路,还不太懂…

Code

#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <algorithm>
using namespace std;
const int N = 100000, NUM = 500000, BIT = 20;

int a[N+1];

int count(int x, int y)
{
    x = a[x] ^ a[y];
    y = 0;
    for(int i=0; i<BIT; ++i)
        y += x >> i & 1;
    return y;
}

int main()
{
    srand((unsigned)time(NULL));
    int t;
    scanf("%d", &t);
    while(t--)
    {
        int n;
        scanf("%d", &n);
        for(int i=1; i<=n; ++i)
            scanf("%X", a+i);
        int ans = BIT;
        for(int i=0, x, y; i<NUM; ++i)
        {
            x = rand() % n + 1;
            y = rand() % n + 1;
            if(x == y)
                continue;
            ans = min(ans, count(x, y));
        }
        printf("%d\n", ans);
    }
    return 0;
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值