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. JAVA I/O流 之入门

    I/O流分类: 根据处理的数据类型不同 字节流 字符流 根据流向不同 输入流 输出流 根据功能不同 节点流:直接与数据源相连,读入或读出. 处理流:直接使用节点流,读写不方便,为了更快的读写文件,才有 ...

  2. 第一个Spring MVC程序

    最近公司项目要开始使用Spring MVC替代Struts2了,就学习了一下Spring MVC的使用.这是第一个Spring mvc程序,分别使用xml和注解两种方式. 一.使用xml格式进行构建 ...

  3. java学习笔记day05

    1.final关键字:防止被继承的类或覆写的方法修改,变量或方法被final定义后  会在内在中存在 特点:   1)可以修饰类.函数.变量.   2)被final修饰的类不可以被继承.   3)被f ...

  4. application,session,cookie三者之间的区别和联系

    application:    程序全局变量对象,对每个用户每个页面都有效 session:    用户全局变量,对于该用户的所有操作过程都有效    session主要是在服务器端用,一般对客户端不 ...

  5. Oozie — What Why and How

    Oozie是什么? Oozie最初是Yahoo!为Hadoop开发的一个工作流调度器,一个工作流有多个Job组成.它允许用户提交由多个Job组成的工作流配置文件,这些Job既可以顺序执行,也可以并行执 ...

  6. typedef和define的作用域

    typedef: 如果放在所有函数之外,它的作用域就是从它定义开始直到文件尾: 如果放在某个函数内,定义域就是从定义开始直到该函数结尾: #define: 不管是在某个函数内,还是在所有函数之外,作用 ...

  7. pcl1.7.2_vs2013_x64工程配置

    pcl1.7.2_vs2013_x64工程配置 C:\Program Files\PCL 1.7.2\include\pcl-1.7;C:\Program Files\PCL 1.7.2\3rdPar ...

  8. android.view.InflateException: Binary XML file line #7: Error inflating class(OOM)

    由于页面含有ImageView引起的内存溢出. 作如下处理:在OnDestroy中 Drawable d = imageView.getDrawable(); if (d != null) d.set ...

  9. css3弹性盒模型(Flexbox)

    Flexbox是布局模块,而不是一个简单的属性,它包含父元素和子元素的属性. Flexbox布局的主体思想是似的元素可以改变大小以适应可用空间,当可用空间变大,Flex元素将伸展大小以填充可用空间,当 ...

  10. PHP 表单防止刷新提交的方法

    当然,最直接的办法就是尽量不要使用自动提交的表单,然而,当我们需要网页主动post表单进行初始化时,就不得不面对这个问题了 -------------------------------------- ...