Hamming Distance

Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 1127 Accepted Submission(s): 425

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
 

题意:就是任意两个16进制字符串XOR,在 所有结果中,求结果以二进制表示时,含1个数最少是多少。

import java.io.*;
import java.util.*;
public class Main {
BufferedReader bu;
PrintWriter pw;
int t,n,min;
public static void main(String[] args) throws IOException{
new Main().work();
}
void work() throws IOException{
bu=new BufferedReader(new InputStreamReader(System.in));
pw=new PrintWriter(new OutputStreamWriter(System.out),true);
t=Integer.parseInt(bu.readLine());
while(t--!=0){
n=Integer.parseInt(bu.readLine());
String str[]=new String[n];
for(int i=0;i<n;i++){
str[i]=bu.readLine();
}
min=0xfffffff;
Random da=new Random(1);
for(int i=1;i<1000000;i++){
int x=da.nextInt()%n;
int y=da.nextInt()%n; if(x!=y&&x>=0&&y>=0){
int num1=Integer.parseInt(str[x],16);
int num2=Integer.parseInt(str[y],16);
int num=num1^num2;
getCount(num);
}
}
pw.println(min);
}
}
void getCount(int num){
int len=0;
int count=0;
while((num>=(1<<len))){
if((num&(1<<len))!=0){
count++;
}
len++;
}
min=Math.min(min,count); }
}

HDU 472 Hamming Distance (随机数)的更多相关文章

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

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

  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 ( 随机算法混过了 )

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

  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(随机数法)

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

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

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

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

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

  8. hdu 4712 Hamming Distance bfs

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

  9. HDU 4712:Hamming Distance

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

随机推荐

  1. Matlab近期用到的函数(持续更新)

    最近任务用到matlab较多,第一版的代码大部分对于矩阵类的计算都是用for循环来完成的,主要是思维还没有适应matlab.看了同事的一份代码后,现在遇到需要循环的地方第一反应就是如何能够用矩阵的直接 ...

  2. struts2 convention-plugin

    导入这个插件,该插件的作用是替换掉struts.xml 原则是没有配置,全是约定 基本步骤 1.新建HomeAction,里面有个execute方法return success,请求home.acti ...

  3. VC中TRACE()的用法

    个人总结:最近看网络编程是碰到了TRACE语句,不知道在哪里输出,查了一晚上资料也没找出来,今天终于在CSDN上找到了,真是个高地方啊,方法如下: 1.在MFC中加入TRACE语句 2.在TOOLS- ...

  4. 初遇Git与MarkDown 文件

    新年好! 昨晚熬夜在学Git,稍微会了一些命令. 推荐大家去try.github.io上学习,这是GitHub提供的网页,它在网页提供了一个“伪”模拟器,根据网页的提示学习命令.网页上说15分钟就能学 ...

  5. Palindrome(最长回文串manacher算法)O(n)

     Palindrome Time Limit:15000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit ...

  6. STL 源代码剖析 算法 stl_algo.h -- partition

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie partition ------------------------------------ ...

  7. java实现文件夹(包括其中的子文件夹、子文件)的复制——递归

    这是学校java课的一道实验题,题目如下:编程,根据指定的源和目标位置,完成指定文件或文件夹(包括其中的子文件夹.子文件)的复制. 以下是我的实现,使用了递归: package com.simon.m ...

  8. BZOJ 1455: 罗马游戏( 配对堆 + 并查集 )

    可并堆水题 --------------------------------------------------------- #include<bits/stdc++.h>   usin ...

  9. pycharm+QT4的helloworld

    # -*- coding: utf-8 -*- from PyQt4 import QtCore, QtGui try: _fromUtf8 = QtCore.QString.fromUtf8 exc ...

  10. hdu3483之二项式展开+矩阵快速幂

    A Very Simple Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Ot ...