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. 语义后承(semantic consequence),句法后承(syntactic consequence),实质蕴含(material implication / material conditional)

    作者:罗心澄链接:https://www.zhihu.com/question/21191299/answer/17469774来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...

  2. std::vector利用swap()函数进行内存的释放【转】

    首先,vector与deque不同,其内存占用空间只会增长,不会减小.比如你首先分配了10,000个字节,然后erase掉后面9,999个,则虽然有效元素只有一个,但是内存占用仍为10,000个.所有 ...

  3. Jquery 获取第一个子元素

    <ul>  <li>John</li>  <li>Karl</li>  <li>Brandon</li></u ...

  4. 给开发者准备的 10 款最好的 jQuery 日历插件[转]

    这篇文章介绍的是 10 款最棒而且又很有用的 jQuery 日历插件,允许开发者们把这些漂亮的日历插件结合到自己的网站中.这些日历插件易用性都很强,轻轻松松的就可以把漂亮的日历插件装饰到你的网站了.希 ...

  5. IE11与Cognos怪谈

    想必大家都知道高版本的IE运行Cognos相关组件的时候要设置兼容性,安全级别相对调低等设置.但是升级了win10系统后,默认的IE版本是IE11的,装了Cognos10.2.1之后,进行了一个常规的 ...

  6. cognos report上钻下钻报表处理方法(2)

    在此之前已经说过了在报表本身单个维度上面的上钻与下钻,本次说的是传递参数追溯到其他报表.比如从部门追溯到部门每一位员工的数据分析, 如图:报表1 点击信托业务一总部跳转到下面的报表2,显示每一位执行经 ...

  7. 将Tp-link无线路由器桥接到Dlink无线路由器上

    笔者家中原有两台笔记本和两台IPad,通过一台Dlink无线路由器(型号DIR-612,以下简称Dlink)上网,Dlink以PPPOE方式连到小区宽带.一直还可以. 后来为了练习Linux,启用了一 ...

  8. C#.NET常见问题(FAQ)-Combobox如何设置不可以编辑

    设置它的DropDownStyle为DropDownList即可     更多教学视频和资料下载,欢迎关注以下信息: 我的优酷空间: http://i.youku.com/acetaohai123   ...

  9. 消息队列的使用场景(转载c)

    作者:ScienJus链接:https://www.zhihu.com/question/34243607/answer/58314162来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业 ...

  10. Java从零开始学二(标识符和关键字)

    标识符.关键字.注释 一.标识符 Java中的包.类.方法.参数和变量的名字由任意顺序的大小字母.数字.下划线(_).和美元符号($)组成, 标识符:不能以数字开头.也不能是JAVA中的保留关键字 如 ...