Hamming Distance

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

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
 
这道题的正常做法是O(n^2)的,很显然这个做法会大大方方的T掉
但是我们可以用随机化,然后悄悄不说话的水过去= =
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <algorithm>
  4. #include <string.h>
  5. #include <time.h>
  6.  
  7. using namespace std;
  8. #define N 100000
  9.  
  10. char str[N+][];
  11. int mark[][]; //make中存 i^j 的1的个数
  12. int arr[]={,,,,,,,,,,,,,,,}; //0-F 中1的个数
  13.  
  14. int charToHex(char ch) //将0-F字符转换成10进制数计算
  15. {
  16. if(isdigit(ch)) return ch-'';
  17. return ch-'A'+;
  18. }
  19.  
  20. void getMark() //求mark数组
  21. {
  22. int i,j,s;
  23. for(i=;i<;i++)
  24. {
  25. for(j=i;j<;j++)
  26. {
  27. s=i^j;
  28. mark[i][j]=mark[j][i]=arr[s];
  29. }
  30. }
  31. }
  32.  
  33. int geths(int x,int y) //求x到y的Hamming distance
  34. {
  35. int i,sum=;
  36. for(i=;i<;i++)
  37. {
  38. int xx = charToHex(str[x][i]);
  39. int yy = charToHex(str[y][i]);
  40. sum+=mark[xx][yy];
  41. }
  42. return sum;
  43. }
  44.  
  45. int main()
  46. {
  47. int t;
  48. getMark();
  49. scanf("%d",&t);
  50. while(t--)
  51. {
  52. int n;
  53. scanf("%d",&n);
  54. int i;
  55. for(i=;i<n;i++)
  56. {
  57. scanf("%s",str[i]);
  58. }
  59. srand(time(NULL));
  60. int x,y,mins=;
  61. for(i=;i<;i++) //随机900000次基本能过,在不超时的前提下,随机次数越多越好
  62. {
  63. x=rand()%n;
  64. y=rand()%n;
  65. if(x==y) continue;
  66. int temp = geths(x,y);
  67. if(mins>temp) mins=temp;
  68. }
  69. printf("%d\n",mins);
  70. }
  71. return ;
  72. }
  73. /*
  74. 2
  75. 2
  76. 12345
  77. 54321
  78. 4
  79. 12345
  80. 6789A
  81. BCDEF
  82. 0137F
  83. */

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

    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 解题报告:输入n个数,用十六进制的方式输入的,任意选择其中的两个数进行异或,求异或后的数用二进制 ...

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

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

  5. HDU 472 Hamming Distance (随机数)

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

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

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

  7. Hamming Distance(随机算法)

    http://acm.hdu.edu.cn/showproblem.php?pid=4712 题意:计算任意两个十六进制的数异或后1的最少个数. 思路:用随机数随机产生两个数作为下标,记录这两个数异或 ...

  8. hdu 4712 Hamming Distance(随机数法)

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

  9. hdu 4712 Hamming Distance bfs

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

随机推荐

  1. URIEncoding与useBodyEncodingForURI 在tomcat中文乱码处理上的区别

    大家知道tomcat5.0开始,对网页的中文字符的post或者get,经常会出现乱码现象. 具体是因为Tomcat默认是按ISO-8859-1进行URL解码,ISO-8859-1并未包括中文字符,这样 ...

  2. c++字节数组转换为整型

    http://bbs.csdn.net/topics/360132089 BYTE data[4]={0x00,0x00,0xe6,0x00};//第一句UINT a11=*(UINT*)data;/ ...

  3. spring boot jpa 多数据源配置

    在实际项目中往往会使用2个数据源,这个时候就需要做额外的配置了.下面的配置在2.0.1.RELEASE 测试通过 1.配置文件 配置两个数据源 spring.datasource.url=jdbc:m ...

  4. 栈应用之 后缀表达式计算 (python 版)

    栈应用之 后缀表达式计算 (python 版) 后缀表达式特别适合计算机处理 1.  中缀表达式.前缀表达式.后缀表达式区别  中缀表达式:(3 - 5) * (6 + 17 * 4) / 3 17 ...

  5. UFLDL 教程学习笔记(二)

    课程链接:http://ufldl.stanford.edu/tutorial/supervised/LogisticRegression/ 这一节主要讲的是梯度的概念,在实验部分,比较之前的线性回归 ...

  6. dpr 与 dproj 有什么区别

  7. sql server 2005/2008R2 报“红叉”错,即“不允许所请求的注册表访问权”的错误

    一.使用报错展示:           1.红叉错: 2.报错文字信息: 解决办法:可以鼠标右键,以管理员的身份运行即可,但这治标不治本,按如下方法可以彻底解决:把“以管理员身份运行此程序”勾上,即可

  8. Rewrite HTTP to HTTPS in Nginx

    1.推荐配置 server { listen 80; server_name example1.com example2.com; return 301 https://$host$request_u ...

  9. hdu 5078(2014鞍山现场赛 I题)

    数据 表示每次到达某个位置的坐标和时间 计算出每对相邻点之间转移的速度(两点间距离距离/相隔时间) 输出最大值 Sample Input252 1 9//t x y3 7 25 9 06 6 37 6 ...

  10. Rookey.Frame之数据库及缓存配置

    上一篇中讨论了Rookey.Frame框架菜单配置功能,这一节我们继续学习Rookey.Frame框架的数据库连接配置. 之前介绍了Rookey.Frame框架支持跨多数据库,并且支持读写分离,不过目 ...