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. hibernate 持久化对象的生命周期 2.1

    持久化对象的生命周期 瞬态(自由态) 表示对象在内存中存在,在数据库中没有数据相关,比如刚刚new出来的一个对象 持久态 持久态指的是持久化对象处于由Hibernate管理的状态,这种状态下持久化对象 ...

  2. cocos2dx进阶学习之CCBI文件

    在马里奥这个游戏里,我们用到了几个ccbi文件,比如蘑菇怪,马里奥等,下面是加载马里奥代码

  3. X Window System介绍

    1.概述     X Window System是1984年由麻省理工学院(MIT)和DEC公司共同开发研究的,是执行在UNIX系统上的视窗系统.严格地说,X Window System并非一个软件, ...

  4. PHP - 直接输出对象的版本问题

  5. Python 第十二篇:HTML基础

    一:基础知识: HTML是英文Hyper Text Mark-up Language(超文本标记语言)的缩写,他是一种制作万维网页面标准语言(标记).相当于定义统一的一套规则,大家都来遵守他,这样就可 ...

  6. 如何借助Motion操控Linux监控摄像头

    介绍 本文介绍如何使用motion来操控Linux下的摄像头. 安装 apt-get install motion 配置文件 输入命令后面的命令编辑配置文件, vim /etc/motion/moti ...

  7. POJ 2187 旋转卡壳 + 水平序 Graham 扫描算法 + 运算符重载

    水平序 Graham 扫描算法: 计算二维凸包的时候可以用到,Graham 扫描算法有水平序和极角序两种. 极角序算法能一次确定整个凸包, 但是计算极角需要用到三角函数,速度较慢,精度较差,特殊情况较 ...

  8. 基于visual Studio2013解决C语言竞赛题之0507筛选素数

     题目

  9. Ganglia 权威指南-安装Ganglia过程

    转自于:http://blog.csdn.net/xxd851116/article/details/21527055 http://www.dataguru.cn/article-3816-1.ht ...

  10. C# 多媒体播放器

    //停止播放 public void stopFile() { axWindowsMediaPlayer1.Ctlcontrols.stop(); } //暂停文件 public void pause ...