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个数最少是多少。

  1. import java.io.*;
  2. import java.util.*;
  3. public class Main {
  4. BufferedReader bu;
  5. PrintWriter pw;
  6. int t,n,min;
  7. public static void main(String[] args) throws IOException{
  8. new Main().work();
  9. }
  10. void work() throws IOException{
  11. bu=new BufferedReader(new InputStreamReader(System.in));
  12. pw=new PrintWriter(new OutputStreamWriter(System.out),true);
  13. t=Integer.parseInt(bu.readLine());
  14. while(t--!=0){
  15. n=Integer.parseInt(bu.readLine());
  16. String str[]=new String[n];
  17. for(int i=0;i<n;i++){
  18. str[i]=bu.readLine();
  19. }
  20. min=0xfffffff;
  21. Random da=new Random(1);
  22. for(int i=1;i<1000000;i++){
  23. int x=da.nextInt()%n;
  24. int y=da.nextInt()%n;
  25.  
  26. if(x!=y&&x>=0&&y>=0){
  27. int num1=Integer.parseInt(str[x],16);
  28. int num2=Integer.parseInt(str[y],16);
  29. int num=num1^num2;
  30. getCount(num);
  31. }
  32. }
  33. pw.println(min);
  34. }
  35. }
  36. void getCount(int num){
  37. int len=0;
  38. int count=0;
  39. while((num>=(1<<len))){
  40. if((num&(1<<len))!=0){
  41. count++;
  42. }
  43. len++;
  44. }
  45. min=Math.min(min,count);
  46.  
  47. }
  48. }

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. asp.net core+ef core

    asp.net core+ef core 官方的文档https://docs.asp.net/en/latest/tutorials/first-mvc-app/start-mvc.html 先来看一 ...

  2. Delphi 中 COM 实现研究手记(一)

    前言 前些日子用 Delphi 写了一个 Windows 外壳扩展程序,大家知道 Windows 外壳扩展实际上就是 COM 的一种应用 -- Shell COM,虽然整个程序写得还算比较顺利,但写完 ...

  3. hibernate 数据关联多对多 4.1

    多对多,必须有一张关系表来维持关系 数据库student,teacher student_teacher 三张表 但是在pojo中只需要建立student和teacher两个类,除非关系表也代表某种业 ...

  4. 在 Azure 网站上使用 Memcached 改进 WordPress

    编辑人员注释:本文章由 Windows Azure 网站团队的项目经理 Sunitha Muthukrishna 和 Windows Azure 网站开发人员体验合作伙伴共同撰写. 您是否希望改善在 ...

  5. 类之string类、Math类、DateTime类

    String类 string a = "abcdef123456"; 注:字符串的长度是从0开始计数的如:0,1,2,3,4,5,6,7,8,9........ a.Length; ...

  6. Xcode7网络限制

    在info.plist添加字段 App Transport Security Settings Allow Arbitrary Loads yes

  7. C#高级编程随笔

    1.把类创作的变量叫做对象2.类就是对象的模版3.类定义了每个对象的数据和功能4.接口不能被实例化,抽象类不能被实例化5.抽象基类可以包含非抽象方法,而接口只能包含抽象方法6.一个类可以实现多个接口7 ...

  8. VMware Workstation9安装Mac OS X10.9系统

    链接地址:http://jingyan.baidu.com/article/aa6a2c142cef740d4c19c426.html VMware Workstation9.0安装Mac OS X1 ...

  9. ListView判断滑动底部

    通过实现OnScrollListener这个接口,然后复写 public abstract void onScroll (AbsListView view, int firstVisibleItem, ...

  10. iOS 开发设计常用软件及工具整理

    1, xCode 2, AppCode 3, Skech 原型设计软件 4, Hype 动画设计工具 5, fontawsome 免费图表 6, Prepo icon, images.catlog 生 ...