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. 如何设置nginx日志格式来查看负载分担结果

     转载:http://www.cnblogs.com/LoveJulin/p/5082363.html nginx配置好负载分担后,测试的时候,如何查看负载分担情况:通过设置nginx日志显示: ng ...

  2. 莫比乌斯函数&莫比乌斯反演

    莫比乌斯函数:http://wenku.baidu.com/view/fbec9c63ba1aa8114431d9ac.html Orz  PoPoQQQ

  3. Objective-C:ARC自动释放对象内存

    ARC是cocoa系统帮你完成对象内存释放的引用计数机制 .h文件 // Person.h // 01-ARC // // Created by ma c on 15/8/13. // Copyrig ...

  4. 游戏:贪吃虫(GreedyMaggot)

    该游戏类似于贪吃蛇,但可以在二维平面上以任意方向前进.当吃到食物手,食物会从虫头向虫尾移动,移到虫尾后,贪吃虫长度会增加.本来给它取名为贪吃蛆的,并且工程的英文名Maggot就是蛆的意思,后来想想有点 ...

  5. 第十一章 企业项目开发--消息队列activemq

    注意:本章代码基于 第十章 企业项目开发--分布式缓存Redis(2) 代码的github地址:https://github.com/zhaojigang/ssmm0 消息队列是分布式系统中实现RPC ...

  6. 几行简单代码实现DIV层上显示Tooltip效果

    最近在做一个项目,要在鼠标移到层上后显示出tip提示,网上找了半天,都很麻烦,就自己修改了一个,记录在下面 测试在IE 7.8.9及 chrome 上没问题. <HTML> <HEA ...

  7. kafka基本原理概述——patition与replication分配

    kafka一直在大数据中承受着数据的压力也扮演着对数据维护转换的角色,下面重点介绍kafka大致组成及其partition副本的分配原则: 文章参考:http://www.linkedkeeper.c ...

  8. com.esotericsoftware.kryo.kryoexception java.util.ConcurentModificationException

    近期 有网友看我的"整合Kafka到Spark Streaming--代码演示样例和挑战"文章, 讲 kafka对象 放到 pool 并通过broadcast广播出去: 然后 在开 ...

  9. SqlServer日常积累(一)

    1. 将一个表的数据插入另一个表 情况一:目标表已存在 (1)如果2张表的字段一致,并且希望插入全部数据,可以用这种方法: Insert Into 目标表 Select * From 来源表; --例 ...

  10. php+tomcat 配置运行环境

    为了学习php,本教程始于:2017.11.16 完成时的截图! 1.首先下载: VC 2015++ 点击下载 2. 把下载好的php复制到本目录,然后解压并且重命名为“php”,如果没有下载php, ...