Problem
acm.hdu.edu.cn/showproblem.php?pid=4712
Reference
多向 bfs 思路
优快云-markdown 用 LaTeX
Meaning
定义两个整数数 a 和 b 的汉明距离为:
a⨁b
的二进制表示中 1 的个数。
给出 n 个 5 位十六进制表示的数,求所有数对的汉明距离中的最小值。
Analysis
暴力会超时。
多次随机选两个不同的数来算海明距离,并更新答案。
试算下概率…
记从 n 个数中任选两个,刚好选到那两个能产生最小海明距离的数(假设只有一对这样的数)的概率是 p,
则
p=1C2n
,
而选不中的概率就是
1−p
,
随机 m 次的话,都选不中的概率就是
(1−p)m
,m很大失败概率就会变得很小,而成功概率
1−(1−p)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;
}

本文介绍了一种求解多个十六进制数间汉明距离最小值的有效算法。通过随机选择两数计算汉明距离并迭代更新最小值,避免了全组合计算带来的效率问题。此外,还探讨了该方法的成功概率计算。
1287

被折叠的 条评论
为什么被折叠?



