Hamming Distance

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

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
 

思路:涨知识,还可以随机;

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. #define pi (4*atan(1.0))
  5. #define eps 1e-14
  6. const int N=1e5+,M=4e6+,inf=1e9+,mod=1e9+;
  7. const ll INF=1e18+;
  8. int getnum(char a)
  9. {
  10. if(a>=''&&a<='')
  11. return a-'';
  12. return a-'A'+;
  13. }
  14. int check(int x,int y)
  15. {
  16. int sum=;
  17. while(x||y)
  18. {
  19. if(x%!=y%)
  20. sum++;
  21. x>>=;
  22. y>>=;
  23. }
  24. return sum;
  25. }
  26. char ch[N][];
  27. int mp[][];
  28. int main(){
  29. for(int i=;i<;i++)
  30. {
  31. for(int t=;t<;t++)
  32. mp[i][t]=check(i,t);
  33. }
  34. int T;
  35. scanf("%d",&T);
  36. while(T--)
  37. {
  38. int x;
  39. scanf("%d",&x);
  40. for(int i=;i<x;i++)
  41. scanf("%s",ch[i]);
  42. int ans=inf;
  43. for(int i=;i<=;i++)
  44. {
  45. int k=;
  46. int n=rand()%x;
  47. int m=rand()%x;
  48. if(n==m)continue;
  49. for(int t=;t<;t++)
  50. k+=mp[getnum(ch[n][t])][getnum(ch[m][t])];
  51. ans=min(ans,k);
  52. }
  53. printf("%d\n",ans);
  54. }
  55. return ;
  56. }

hdu 4712 Hamming Distance 随机的更多相关文章

  1. hdu 4712 Hamming Distance ( 随机算法混过了 )

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

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

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

  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. hdu 4712 Hamming Distance(随机数法)

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

  7. hdu 4712 Hamming Distance bfs

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

  8. 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 ...

  9. HDU 472 Hamming Distance (随机数)

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

随机推荐

  1. resultMap / resultType

    ===================resultMap:实体类的属性和通过resultMap映射后的property属性一致 <resultMap id="workerSelect& ...

  2. 在keil 4中添加stc系列芯片的方法--【sky原创】

    在keil 4中添加stc系列芯片的方法: 1.从官网下载uv3.cdb的文件网址是:http://www.stcmcu.com/ 2.下载好后把uv3.cdb文件改成STC.cdb:3. 然后将[S ...

  3. windows win10上传文件到linux服务器

    1.最直接当然使用终端secucrt和xshell putty之类的,然后使用sz rz 2.如果服务器端不支持sz rz可以使用scp命令,下面这个pscp.exe就是支持scp的,基于ssh,很好 ...

  4. C#:简单线程样例

    1.定义线程类及内部事件 using System; using System.Collections.Generic; using System.Text; using System.Threadi ...

  5. css样式(二)(伪类 | 伪元素 | 导航 | 图像拼合 | 属性选择器 )

    一.rgb值: RGBA 颜色 RGBA 颜色值得到以下浏览器的支持:IE9+.Firefox +.Chrome.Safari 以及 Opera +. RGBA 颜色值是 RGB 颜色值的扩展,带有一 ...

  6. android studio自动导包

    http://blog.csdn.net/buaaroid/article/details/44979629 关于导包的设置以上博文解释的很清楚,在此主要强调下这一句: Add unambiguous ...

  7. StringUtils 帮助类所涉及的方法

    /*1.字符串以prefix开始*/StringUtils.startsWith("sssdf","");//结果是:trueStringUtils.start ...

  8. Cocos2dx框架常用单词(一)

    收集了一些Cocos2dx里面主要单词的翻译. Toggle:切换Finite:有限Instant:瞬时interval:间隔Flip:翻转place:座位,放置Target:目标reverse:反向 ...

  9. Linux hrtimer分析(2)

    http://blog.csdn.net/angle_birds/article/details/17375901 本文介绍Linux2.6.29中,配置高精度模式的hrtimer与未配置高精度模式时 ...

  10. YTU 2991: 链表节点逆置(线性表)

    2991: 链表节点逆置(线性表) 时间限制: 1 Sec  内存限制: 128 MB 提交: 14  解决: 6 题目描述 设计一个算法,将一个带头节点的数据域依次为a1,a2,-,an(n> ...