本文出自: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. 关于RtlInitUnicodeString感想

    01 VOID RtlInitUnicodeString (OUT PUNICODE_STRING DestinationString,IN PCWSTR SourceString OPTIONAL) ...

  2. synapse socket总结一:服务器模型

    synapse (http://synapse.ararat.cz/doku.php)的源码简洁明了,属于轻量级的阻塞式socket通讯组件包,更多的功能需要自己基于它的基础上去封装实现.相对于ind ...

  3. sharePoint常用命令

    New-SPStateServiceDatabase -Name "StateServiceDatabase" | New-SPStateServiceApplication -N ...

  4. Android学习笔记之View(二)

    View加载的流程之测量:rootView调用measure()→onMeasure(): measure()是final方法,表明Android不想让开发者去修改measure的框架,开发者可以on ...

  5. Linux命令: ln

    每天一个linux命令(35):ln 命令 实例1:给文件创建软链接 命令: ln -s log2013.log link2013 输出: [root@localhost test]# ll -rw- ...

  6. BZOJ 1305: [CQOI2009]dance跳舞( 最大流 )

    云神代码很短...0 ms过的...看了代码 , 大概是贪心... orz 我不会证 数据这么小乱搞就可以了吧... ←_← 这道题网络流还是可以写的... 既然限制了最多只能和 k 个不喜欢的人da ...

  7. 基于visual Studio2013解决C语言竞赛题之0809链表排序

     题目

  8. Login oracle for external authenticate

    Generally, we can login the oracle by os authentication, if we login os in a remote machine and make ...

  9. PHP脚本监控程序

    #!/bin/sh # Find ip IP=`/sbin/ifconfig eth1 | grep 'inet addr' | awk '{ print substr($2, index($2, & ...

  10. Java 螺纹第三版 第三章数据同步 读书笔记

    多线程间共享数据问题 一.Synchronizedkeyword      atomic一词与"原子"无关,它以前被觉得是物质的最小的单元,不能再被拆解成更小的部分.      当 ...