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. openshift 添加cron定时任务

    一般linux添加cron任务是在/etc/crontab,但是由于openshift的权限木有这么开放,所以如果需要设置定时任务的话,需要在如下的文件夹下添加你的sh文件,因为我需要的是每天运行一次 ...

  2. Python3.6学习笔记(三)

    面向对象编程 面向对象编程 Object Oriented Programming 简称 OOP,是一种程序设计思想.OOP把对象作为程序的基本单元,一个对象包含了数据和操作数据的函数. 面向过程的程 ...

  3. strcmp实现

    #include<stdio.h> #include<assert.h> int my_strcmp(const char*a,const char*b) { while(*a ...

  4. UCN(User-Centric Networks,用户中心网络)

    UCN(User-Centric Networks,以用户为中心的网络)是下一代移动通信网络(5G)的发展方向,目前尚处于初级发展阶段.2016年11月,IEEE SDN研究组(聚焦研发SDN.NFV ...

  5. 分布式缓存系统Memcached在Asp.net下的应用

    Memcached 是一个高性能的分布式内存对象缓存系统.用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来降低读取数据库的次数,从而提高动态.数据库驱动站点的速度. Memcache ...

  6. 如何在Window 7 64位 PL/SQL 访问oracle 数据库

    一般 PLSQL Developer 没有64位版本,所以在64位系统上运行该程链接64位Oracle时就会报错.解决的方法如下: 第零步:在windows 7 中安装Oracle 11g 64 数据 ...

  7. go语言基础之数组做函数参数是值拷贝

    1.数组做函数参数是值拷贝 示例: package main //必须有个main包 import "fmt" //数组做函数参数,它是值传递 //实参数组的每个元素给形参数组拷贝 ...

  8. Android -- Camera源码简析,启动流程

    com.android.camera.Camera.java,主要的实现Activity,继承于ActivityBase. ActivityBase 在ActivityBase中执行流程: onCre ...

  9. (C++)i++和++i,哪个效率高一些

    在看<程序员面试笔试宝典>时,发现了这样一个问题,书中只给出了++i的效率高一些,但并没有给出具体的解释和说明. 在网上找到下面的答案: 1.从高级层面上解释 ++i 是i=i+1,表达式 ...

  10. 【Window OS】”对于目标文件系统,文件XXXXX过大“导致无法进行文件操作的解决方法

    问题原因:这是目标文件系统不支持这么大的文件的操作问题.例如:目标文件系统的格式是FAT32,FAT32最大支持4G,如果你要进行发送或粘贴4G以上的文件就会出现这个问题. 解决办法:把目标文件系统的 ...