本文出自: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;
}

HDU4712-----Hamming Distance------超级大水题的更多相关文章

  1. hdu4712 Hamming Distance

    Hamming Distance Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) To ...

  2. HDU4712 Hamming Distance (随机化)

    link:http://acm.hdu.edu.cn/showproblem.php?pid=4712 题意:给1e5个数字,输出这些数中,最小的海明码距离. 思路:距离的范围是0到20.而且每个数的 ...

  3. hdu 1228 A+B 字符串处理 超级大水题

    中文意思不解释. 很水,我本来想用switch处理字符串,然后编译不通过...原来switch只能处理整数型的啊,我都忘了. 然后就有了很挫的一大串if代码了... 代码: #include < ...

  4. hdu 1229 超级大水题

      Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Submit Status Desc ...

  5. hdu 4548 美素数 超级大水题

    美素数 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Submis ...

  6. Lweb and String 超级大水题

    Lweb and String Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  7. Total Hamming Distance

    The Hamming distance between two integers is the number of positions at which the corresponding bits ...

  8. 461. Hamming Distance and 477. Total Hamming Distance in Python

    题目: The Hamming distance between two integers is the number of positions at which the corresponding ...

  9. LeetCode Total Hamming Distance

    原题链接在这里:https://leetcode.com/problems/total-hamming-distance/ 题目: The Hamming distance between two i ...

  10. LeetCode Hamming Distance

    原题链接在这里:https://leetcode.com/problems/hamming-distance/ 题目: The Hamming distance between two integer ...

随机推荐

  1. phpMyAdmin 手动输入数据库服务器IP

    1 在phpMyAdmin安装目录下查找config.inc.php,如没有则拷贝config.sample.inc.php为config.inc.php 2 修改config.inc.php,加入$ ...

  2. 基于visual Studio2013解决算法导论之051区间树

     题目 区间树 解决代码及点评 #include <stdio.h> #include <string.h> #include <iostream> #def ...

  3. win8安装驱动提示文件哈希值不在指定的目录文件中,此文件可能已损坏或被篡改解决办法

    解决办法: 1. 按快捷键win+R 打开运行命令 2. (请先看完后面的再操作!!)运行输入 shutdown.exe /r /o /f /t 00 3. 点击确定 4. 系统将重启 5. 重启后点 ...

  4. eclipse中build workspace的相关优化

    网上流传的各种的eclipse的调优的方法都大同小异,但是调优的基本上针对eclipse或者myclipse的本身,比如关掉validate和启动项,文件拼写,和自动构建等,调过之后,等个eclips ...

  5. LeetCodeOJ. String to Integer (atoi)

    试题请參见: https://oj.leetcode.com/problems/string-to-integer-atoi/ 题目概述 Implement atoi to convert a str ...

  6. SignalR系列教程:服务器广播与主动数据推送

    本篇是本系列入门篇的最后一遍,由于工作关系,接触SignalR的时间不是很多.等下次有空的话我会写一个利用“SignalR”开发一个在线聊天室的系列博文.近期的话我更偏向于更新框架设计相关的文章,到时 ...

  7. QThread 与 QObject的关系(QObject可以用于多线程,可以发送信号调用存在于其他线程的slot函数,但GUI类不可重入)

    QThread 继承 QObject..它可以发送started和finished信号,也提供了一些slot函数. QObject.可以用于多线程,可以发送信号调用存在于其他线程的slot函数,也可以 ...

  8. ios开发问题:添加库和复制其它工程文件编译错误问题

    首先添加库问题: 选择页签Build Phases->Link Binary With Libraries点+添加库 然后command+shift+k清除历史debug的垃圾 复制其它工程文件 ...

  9. 关于运行robotium提示连接不上jar问题

    robotium运行测试helloworld报错: java.lang.NoClassDefFoundError: com.jayway.android.robotium.solo.Solo at c ...

  10. 向架构师进军---&gt;系统架构设计基础知识

    假设你对项目管理.系统架构有兴趣,请加微信订阅号“softjg”,增加这个PM.架构师的大家庭 在解说系统架构设计之前,有必要补充一下架构相关的概念,因此本博文主要讲述架构.架构师和架构设计等相关的概 ...