Hamming Distance

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

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)16进制的输入输出......
(2)查了以下资料,有关汉明距离的算法:

对于两个字串...可以是数组,字符,求其汉明距离的是对两个字串求异或^。。。

该部分的代码为:

/*
ussigned dist(unsigned a, unsigned b)
{
int val=a^b,da=0;
while(val)
{
val ^= val -1;
da++;
}
return 0;
}
*/

随机算法需要知道的几个函数srand(),rand(),---》头文件为stdlib.h

时间的头文件为time.h-----time();

所以代码为:

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<ctime>
#include<algorithm>
#define maxn 100000
using namespace std;
int dist(int a,int b) //gongshi
{
int val=a^b,distance=;
while(val)
{
++distance;
val &= val - ;
}
return distance;
}
int Hex[maxn+];
int main()
{
int t,i,n,min,x,y,c,cnt;
//time_t t1;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i=;i<n;i++)
scanf("%X",&Hex[i]);
srand((unsigned)time(NULL));
for(cnt=i=;i<maxn;i++)
{
x=rand()%n;
y=rand()%n;
if(x!=y)
{
c=dist(Hex[x],Hex[y]);
if(cnt==||min>c)
min=c,cnt=;
else
if(min==) break;
}
}
printf("%d\n",min);
}
return ;
}

HDUOJ---Hamming Distance(4712)的更多相关文章

  1. Leetcode#461. Hamming Distance(汉明距离)

    题目描述 两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目. 给出两个整数 x 和 y,计算它们之间的汉明距离. 注意: 0 ≤ x, y < 231. 示例: 输入: x = ...

  2. 461. Hamming Distance(leetcode)

    The Hamming distance between two integers is the number of positions at which the corresponding bits ...

  3. HDU 472 Hamming Distance (随机数)

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

  4. LeetCode 461. Hamming Distance (汉明距离)

    The Hamming distance between two integers is the number of positions at which the corresponding bits ...

  5. 12.Hamming Distance(汉明距离)

    Level:   Easy 题目描述: The Hamming distance between two integers is the number of positions at which th ...

  6. [LeetCode] 477. Total Hamming Distance(位操作)

    传送门 Description The Hamming distance between two integers is the number of positions at which the co ...

  7. [LeetCode] 461. Hamming Distance(位操作)

    传送门 Description The Hamming distance between two integers is the number of positions at which the co ...

  8. hamming distance(汉明距离)

    看knn算法时无意间发现这个算法,但是维基上有错误的示例和python代码...因为汉明距离并不是求相同长度字符串(或相同长度的整数)之间的字符(或数位)差异个数. 正确的详见:https://en. ...

  9. 从0开始的LeetCode生活—461-Hamming Distance(汉明距离)

    题目: The Hamming distance between two integers is the number of positions at which the corresponding ...

随机推荐

  1. iOS:NSDate的主要几种时间形式

    NSDate:时间的获取和操作 1.获取当前时间 //获取当前日期 NSDate *date = sender.date; NSLog(@"%@",date); 2.将date转换 ...

  2. AS .ignore插件 忽略文件

    AS自带的.ignore文件 在AS中新建项目时,默认会创建一个.ignore文件,其中默认忽略的是 *.iml .gradle /local.properties /.idea/workspace. ...

  3. HDU1161 Eddy&#39;s mistakes

    Eddy's mistakes Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  4. ComboBoxEdit 数据绑定 使用模板

    1)定义模板资源 <UserControl.Resources> <local:StatusConvertor x:Key="StatusConvertor"&g ...

  5. (转)ngui3.5.7 版本Scroll View实现方法

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://xyo123.blog.51cto.com/6369437/1405861 现在网 ...

  6. Tomcat7出现HTTP Status 500 - java.lang.ClassCastException: org.apache.jasper.el.ELContextImpl cannot be cast to org.apache.jasper.el.ELContextImpl的解决

    今天在Tomcat7上发布了一个war,过一阵子发现localhost:8080都进不去了.在浏览器输入http://localhost:8080出现如下内容: HTTP Status 500 - j ...

  7. Solr添加SolrDocument报错

    今天写了一个solr入库接口,使用了SolrServer.addBean接口,结果报错:Caused by: org.apache.solr.client.solrj.impl.HttpSolrSer ...

  8. Chrome浏览器内嵌的各种手机模拟器

    打开chrome的控制台标签,然后,点击simulator子标签页,选择需要的手机即可,如下图: 模拟器如下: 阅读原文:Chrome浏览器内嵌的各种手机模拟器

  9. hiberante 二级缓存设置

    <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...

  10. STL - 容器 - Map(二)

    把Map用作关联式数组 MapAdvanceTest.cpp #include <map> #include <string> #include <iostream> ...