Hamming Distance

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 499    Accepted Submission(s): 163
Problem Description
(From wikipedia) For binary strings a and b the Hamming distance is equal to the number of ones in a XOR b. For calculating Hamming distance between two strings a and b, they must have equal length.

Now given N different binary strings, please calculate the minimum Hamming distance between every pair of strings.

 
Input
The first line of the input is an integer T, the number of test cases.(0<T<=20) Then T test case followed. The first line of each test case is an integer N (2<=N<=100000), the number of different binary strings. Then N lines followed, each of the next N line is a string consist of five characters. Each character is '0'-'9' or 'A'-'F', it represents the hexadecimal code of the binary string. For example, the hexadecimal code "12345" represents binary string "00010010001101000101".
 
Output
For each test case, output the minimum Hamming distance between every pair of strings.

 
Sample Input
2
2
12345
54321
4
12345
6789A
BCDEF
0137F
 
Sample Output
6
7
 
Source
 

ps:不知道正解是怎样做,用随机算法水过了。
思路:
答案的范围不大,0~20,数据不强,可以考虑随机算法。

代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <algorithm>
#define maxn 105
#define maxx 100005
#define INF 0x3f3f3f3f
using namespace std; int n,m,ans;
int xr[maxn][maxn];
int a[]= {0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4};
char s[maxx][6]; int cal(int k1,int k2)
{
int i,j,t=0,x,y;
for(i=0; i<5; i++)
{
x=s[k1][i];
if(x>='A'&&x<='F') x=x-'A'+10;
else x=x-'0';
y=s[k2][i];
if(y>='A'&&y<='F') y=y-'A'+10;
else y=y-'0';
t+=xr[x][y];
}
return t;
}
void solve()
{
int i,j,t,x,y;
srand((unsigned)time(NULL));
for(i=1; i<=100000; i++)
{
x=rand()%n;
y=rand()%n;
if(x==y) continue ;
t=cal(x,y);
if(ans>t) ans=t;
if(!ans) return ;
}
}
int main()
{
int i,j,t;
for(i=0; i<16; i++)
{
for(j=0; j<16; j++)
{
xr[i][j]=a[i^j];
}
}
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i=0; i<n; i++)
{
scanf("%s",s[i]);
}
ans=INF;
solve();
printf("%d\n",ans);
}
return 0;
}


hdu 4712 Hamming Distance ( 随机算法混过了 )的更多相关文章

  1. HDU 4712 Hamming Distance(随机算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4712 解题报告:输入n个数,用十六进制的方式输入的,任意选择其中的两个数进行异或,求异或后的数用二进制 ...

  2. hdu 4712 Hamming Distance 随机

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

  3. hdu 4712 Hamming Distance(随机函数暴力)

    http://acm.hdu.edu.cn/showproblem.php?pid=4712 Hamming Distance Time Limit: 6000/3000 MS (Java/Other ...

  4. HDU 4217 Hamming Distance 随机化水过去

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

  5. HDU 4712 Hamming Distance(随机算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4712 题目大意:任意两个数按位异或后二进制中含1的个数被称为海明距离,给定n个数,求出任意其中两个最小 ...

  6. Hamming Distance(随机算法)

    http://acm.hdu.edu.cn/showproblem.php?pid=4712 题意:计算任意两个十六进制的数异或后1的最少个数. 思路:用随机数随机产生两个数作为下标,记录这两个数异或 ...

  7. hdu 4712 Hamming Distance(随机数法)

    d.汉明距离是使用在数据传输差错控制编码里面的,汉明距离是一个概念,它表示两个(相同长度)字对应位不同的数量, 我们以d(x,y)表示两个字x,y之间的汉明距离.对两个字符串进行异或运算,并统计结果为 ...

  8. hdu 4712 Hamming Distance bfs

    我的做法,多次宽搜,因为后面的搜索扩展的节点会比较少,所以复杂度还是不需要太悲观的,然后加上一开始对答案的估计,用估计值来剪枝,就可以ac了. #include <iostream> #i ...

  9. hduoj 4712 Hamming Distance 2013 ACM/ICPC Asia Regional Online —— Warmup

    http://acm.hdu.edu.cn/showproblem.php?pid=4712 Hamming Distance Time Limit: 6000/3000 MS (Java/Other ...

随机推荐

  1. JAXB 注解

    JAXB(Java API for XML Binding),它提供了一个便捷的方式高速Java对象XML转变.于JAX-WS(Java的WebService规范之中的一个)中,JDK1.6 自带的版 ...

  2. linux下ACE使用epoll

    select和epoll的比较就不用多说了.ACE在linux下默认使用select来实现Reactor的.如何在linux下让ACE使用epoll. 1.加一个编译宏,告诉ACE不要使用默认的sel ...

  3. C# 枚举运用"位"操作和"或"操作

    定义: /// <summary> /// The js function type(the same as name). /// </summary> [Flags] pub ...

  4. log4net使用简介

    平常我们在开发网站时,有一些比较重要的地方需要添加日志记录.一般日志记录分为两种:1)在数据库中添加一张日志表,用来记录用户操作并给用户提醒(用户可以看到). 2)在系统中添加一个日志文件,用来记录一 ...

  5. Json.Net介绍及实例

    本系列教程假设读者已经对Json有一定的了解,关于Json在这里不多说.本系列教程希望能对读者开发涉及到Json的.Net项目有一定的帮助.本系列教程是根据官方文档资料和自己项目应用汇总而成.如果觉得 ...

  6. mvc 笔记

    @{ ViewBag.Title = "主页"; } <div> 这里就是渲染Body啦.~~不需要写神马<asp:Content />,其实因为Rende ...

  7. 【转载】Xcode6中添加pch文件

    //原文地址:http://www.cnblogs.com/YouXianMing/p/3989155.html 1. 新建工程: 2. 创建pch文件:cmd+n->other->PCH ...

  8. 004 range的用法

  9. C# 读取xml节点类容

    这是一个测试节点类容的获取 这是控制台代码部分 注意的应用文件 :using.system.Xml using System; using System.Collections.Generic; us ...

  10. win7使用右键导致死机、假死、explorer无法响应的解决方法

    右键引起explorer无法响应,奔溃,主要是由于COMCTL32.DLL和COMCTL21.OCX文件引起的 描述:comctl32.dll是Windows应用程序公用GUI图形用户界面模块.报告提 ...