首页 技术 正文
技术 2022年11月21日
0 收藏 702 点赞 2,497 浏览 1014 个字

本文出自:http://blog.csdn.net/dr5459

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4712

题目意思:

海明距离:任意两个树异或后二进制含1的个数

要你求出最小的海明距离

解题思路:

因为数的格式是固定的,所以可以预处理16进制中任意两个数的异或1的个数

这样在求的时候,可以在O(5)内求出

至于怎么去求所有的,看的群里的思路,随机10W次,而且要保证随机的两个数不同,我随机了6W次

然后就A了,就A了,真的被吓到了,下面上代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;int cmp[16][16];char data[1000005][6];int fun(int q,int w)
{
int a,b;
int ret = 0;
for(int i=0;i<5;i++)
{
char x = data[q][i];
char y = data[w][i];
if(x>='0' && x<='9')
a=x-'0';
else
a=x-'A'+10; if(y>='0' && y<='9')
b=y-'0';
else
b=y-'A'+10; ret += cmp[a][b];
}
return ret;
}int main()
{
for(int i=0;i<=15;i++)
{
for(int j=0;j<=15;j++)
{
int ans=0;
int tmp = i^j;
for(int k=0;k<4;k++)
if((1<<k) & tmp)
ans++;
//cout<<i<<" "<<j<<" "<<ans<<endl;
cmp[i][j] = ans;
}
} int n;
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%s",data[i]); int ans = 0x3f3f3f3f;
srand( (unsigned)time( NULL ) );
for(int i=1;i<=60000;i++)
{
int a = rand()%n+1;
int b = rand()%n+1;
if(a==b)
b = b%n+1;
int tmp = fun(a,b);
if(tmp < ans)
ans = tmp;
} cout<<ans<<endl;
} return 0;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,893
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,422
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,240
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,054
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,683
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,720