Hamming Distance

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

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
 
Recommend
liuyiding
用的预处理,随机算法,还是快了很多,主要用的原理也就是a^b=c,则c^b=a;这一点,就可以预处理,得出结果,我们可以看出最坏的复杂度,就是n*2^20,从理论上说,也不比n*n的复杂度,要好到哪里,但是,我们可以发现如果,n很大,那么结果就一定很小,这样,就可以得到最优结果后就退出了,别的什么bfs dfs,也是这种算法的变形,其次我们这题可以用随算法,枚举10w次就可以了,但个人认为不太好!
#include <iostream>
#include <stdio.h>
#include <vector>
#include <string.h>
using namespace std;
#define MAXN 100050
vector<int > vec[23];
int hash[1<<21],pri[MAXN];
char str[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
void init()
{
int i;
for(i=0;i<=21;i++)
vec[i].clear();
int ii=1<<21;
for(i=0;i<ii;i++)
{
int ans=0;
for(int j=0;j<21;j++)
{
if(i&(1<<j))ans++;
}
vec[ans].push_back(i);
}
}
int main()
{
init();
int tcase,i,j,k,n;
char str[20],c;
scanf("%d",&tcase);
while(tcase--)
{
memset(hash,0,sizeof(hash));
scanf("%d",&n);
bool flag=true;
for(i=0;i<n;i++)
{
int ans=0;
scanf("%X",&pri[i]);
// printf("%d f\n",pri[i]);
hash[pri[i]]++;
if(flag&&hash[pri[i]]>=2)
flag=false; }
if(!flag)
{
printf("0\n");
continue;
}
for(i=1;i<=20;i++)
{
for(j=0;j<n;j++)
for(k=0;k<vec[i].size();k++)
{
if(hash[pri[j]^vec[i][k]])
{
printf("%d\n",i);
goto my;
}
}
}
my:; }
return 0;
}

hdu4712 Hamming Distance的更多相关文章

  1. HDU4712 Hamming Distance (随机化)

    link:http://acm.hdu.edu.cn/showproblem.php?pid=4712 题意:给1e5个数字,输出这些数中,最小的海明码距离. 思路:距离的范围是0到20.而且每个数的 ...

  2. [LeetCode] Total Hamming Distance 全部汉明距离

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

  3. [LeetCode] Hamming Distance 汉明距离

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

  4. Total Hamming Distance

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

  5. Hamming Distance

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

  6. 461. Hamming Distance and 477. Total Hamming Distance in Python

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

  7. LeetCode Total Hamming Distance

    原题链接在这里:https://leetcode.com/problems/total-hamming-distance/ 题目: The Hamming distance between two i ...

  8. LeetCode Hamming Distance

    原题链接在这里:https://leetcode.com/problems/hamming-distance/ 题目: The Hamming distance between two integer ...

  9. LeetCode:461. Hamming Distance

    package BitManipulation; //Question 461. Hamming Distance /* The Hamming distance between two intege ...

随机推荐

  1. BZOJ 1631: [Usaco2007 Feb]Cow Party

    题目 1631: [Usaco2007 Feb]Cow Party Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 491  Solved: 362[Sub ...

  2. Wiki: HSL和HSV色彩空间

    HSL 和 HSV(也叫做 HSB)是对RGB 色彩空间中点的两种有关系的表示,它们尝试描述比 RGB 更准确的感知颜色联系,并仍保持在计算上简单.HSL 表示 hue(色相).saturation( ...

  3. iOS7,8 presentViewController 执行慢

    解决办法: 1, 使用GCD用主线程跳转 dispatch_async(dispatch_get_main_queue(), ^{ //跳转代码 ... }); 2, 召唤主线程, 使用perform ...

  4. 135实例——add_4

    自我检讨,基础太差了.找了一本135个实例的pdf文档,一个个往后面编吧,希望能巩固一下基础 //date : 2013/8/19 //designer :pengxiaoen //function ...

  5. grid.Column INT 所对应的文本

    grid.Column("RoleId", "角色名称", (p) => { var role = string.Empty; if (p.RoleId ...

  6. 基于Sql Server 2008的分布式数据库的实践(二)

    原文 基于Sql Server 2008的分布式数据库的实践(二) 从Win7连接Win2003的Sql Server 2008 1.新建链接服务器链接到Win2003的Sql Server 2008 ...

  7. springmvc+mybatis+redis(转)

    最近在学习redis的使用方法,它的本地使用方法比较简单,只需要先启动Redis服务器,然后运行测试代码即可.但是现在我想要在网站上访问数据库的时候采用Redis缓存,问题就出来了.要么是缓存直接失效 ...

  8. c语言memset详解

    void *memset(void *s, int ch, size_t n);(int ch可以是char或int) 将s所指向的某一块内存中的每个字节的内容全部设置为ch指定的ASCII值, 块的 ...

  9. grep egrep fgrep命令

    一.grep.egrep.fgrep命令 本文中主要介绍了linux系统下grep egrep fgrep命令和正则表达式的基本参数和使用格式.方法.(注释:文中fg代表例子,) 1.1.基本定义: ...

  10. 算法学习笔记(三) 最短路 Dijkstra 和 Floyd 算法

    图论中一个经典问题就是求最短路.最为基础和最为经典的算法莫过于 Dijkstra 和 Floyd 算法,一个是贪心算法,一个是动态规划.这也是算法中的两大经典代表.用一个简单图在纸上一步一步演算,也是 ...