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. 细说UI线程和Windows消息队列(经典)

    在Windows应用程序中,窗体是由一种称为“UI线程(User Interface Thread)”的特殊类型的线程创建的. 首先,UI线程是一种“线程”,所以它具有一个线程应该具有的所有特征,比如 ...

  2. 数据结构——栈(Stacks)

    栈遵循LIFO ( last in first out) 即后入先出原则 栈结构类似于叠盘子 后叠上去的要先拿走 才能拿到下面的盘子 因此stack是一种访问受限的线性存储结构 用单向链表的结构来存储 ...

  3. (3)选择元素——(2)文档对象模型(The Document Object Model)

    One of the most powerful aspects of jQuery is its ability to make selecting elements in the DOM easy ...

  4. Surround the Trees(凸包求周长)

    Surround the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  5. sublime2/3自总结经常使用快捷键(2的居多)

    Ctrl+D 选词 (重复按快捷键,就可以继续向下同一时候选中下一个同样的文本进行同一时候编辑) Ctrl+鼠标左键 能够同一时候选择要编辑的多处文本 Shift+鼠标右键(或使用鼠标中键)能够用鼠标 ...

  6. server配置学习 ---- 关闭防火墙

    iptables 一种网络防火墙,在LINUX下使用,RedHat9.0版本号以上自带. 它能够实现NAT转换.能够做上网代理. 首先对于server的配置第一步来说就是关闭防火墙.在没有图形化中的l ...

  7. 打印NSLog分类 Foundation+Log.m

    #import <Foundation/Foundation.h> #import <UIKit/UIKit.h> @implementation UIView(Log) + ...

  8. CMarkUp接口说明

    CMarkup是一个小型XML的分析器,实现语言是C++,英文版的接口说明地址为:http://www.firstobject.com/dn_markupmethods.htm 有厉害的网友已经翻译出 ...

  9. Visual Studio 2012中编写C程序

    换了win7系统后,突然发现VC++6.0不兼容了,我听说有的同学的行,反正我是不行. 那就用VS2012呗.... 我们来看看怎么用: 打开文件->新建->项目,新建一个项目 选择win ...

  10. android 从服务器上获取APK并下载安装

    简单的为新手做个分享.    网上有些资料,不过都是很零散,或是很乱的,有的人说看不懂.    一直有新手说 做到服务器更新APK时没有思路,这里做个简单的分享,希望有不同思路的可以讨论.     下 ...