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. Extjs实现进度条

    做Extjs开发中,往往后台程序可能要执行一段时间才能得到返回结果,加入进度条可以提高客户体验度,以下为两种便捷的方式: 1.提交数据前用Ext.Msg.wait('提示','正在处理数据,请稍候') ...

  2. Perl Symbolic Reference

    看一些模块的代码,很多时候通过*glob的方式来改变变量或者函数,这种方法称为Symbolic reference. 首先看一下*glob的结构,这个在之前的博文已经讲过,不做细述: SV = PVG ...

  3. java Socket 使用注意

    Socket s = new Socket(ia, port); BufferedOutputStream bufOut = new BufferedOutputStream(s.getOutputS ...

  4. 网络编程API-下 (I/O复用函数)

    IO复用是Linux中的IO模型之中的一个,IO复用就是进程预先告诉内核须要监视的IO条件,使得内核一旦发现进程指定的一个或多个IO条件就绪,就通过进程进程处理.从而不会在单个IO上堵塞了. Linu ...

  5. HDU 5792 World is Exploding

    题意: 给出n代表序列的长度,接下来给出序列A.找出abcd满足abcd互不相等1<=a<b<c<d<=n的同时A[a]<A[b],A[c]>A[d],问这样 ...

  6. iOS开发那些事儿(一)轮播器

    前言 市面上绝大部分的APP被打开之后映入眼帘的都是一个美轮美奂的轮播器,所以能做出一个符合需求.高效的轮播器成为了一个程序员的必备技能.所以今天的这篇博客就来谈谈轮播器这个看似简单的控件其中蕴含的道 ...

  7. hadoop压缩配置

    为何要使用压缩,压缩可以是文件的大小减小很多,节省空间:另外压缩后的文件在传输时更节省带宽. 所需软件: 1)lzo 2)hadoop-lzo 3)maven 安装编译: 1)lzo wget htt ...

  8. select语句后面加上for update的作用

    Select…For Update语句的语法与select语句相同,只是在select语句的后面加FOR UPDATE [NOWAIT]子句. 该语句用来锁定特定的行(如果有where子句,就是满足w ...

  9. 数据库 count和sum区别

    最近备考软考,复习数据库时候,发现count和sum,貌似差不错.就仔细查了查. 表 人 id name number 1 贱人 3 2 好人 4 select count(number) from ...

  10. Mobile Matrices

    This is an attempt to compile a list of relevant specifications for all modern smart phones and mobi ...