思路:当时黄建说可以试下随机算法,可是当时没坚持写,一直在找有什么贪心之类的策略。随机10W组就可以过了,呵呵。
貌似网上有正规一点,用搜索的做法吧。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
int a[100001];
unsigned hamdist(unsigned x, unsigned y)
{
unsigned dist = 0, val = x ^ y; // XOR
// Count the number of set bits
while(val)
{
++dist;
val &= val - 1;
}
return dist;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%X",&a[i]);
int ans=30;
int test=100000;
while(test--)
{
int x=rand()%n;
int y=rand()%n;
while(y==x)
y=rand()%n;
ans=min(ans,(int)hamdist(a[x],a[y]));
}
printf("%d\n",ans);
}
return 0;
}