Hamming Distance

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

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
 
这道题的正常做法是O(n^2)的,很显然这个做法会大大方方的T掉
但是我们可以用随机化,然后悄悄不说话的水过去= =
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <time.h> using namespace std;
#define N 100000 char str[N+][];
int mark[][]; //make中存 i^j 的1的个数
int arr[]={,,,,,,,,,,,,,,,}; //0-F 中1的个数 int charToHex(char ch) //将0-F字符转换成10进制数计算
{
if(isdigit(ch)) return ch-'';
return ch-'A'+;
} void getMark() //求mark数组
{
int i,j,s;
for(i=;i<;i++)
{
for(j=i;j<;j++)
{
s=i^j;
mark[i][j]=mark[j][i]=arr[s];
}
}
} int geths(int x,int y) //求x到y的Hamming distance
{
int i,sum=;
for(i=;i<;i++)
{
int xx = charToHex(str[x][i]);
int yy = charToHex(str[y][i]);
sum+=mark[xx][yy];
}
return sum;
} int main()
{
int t;
getMark();
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
int i;
for(i=;i<n;i++)
{
scanf("%s",str[i]);
}
srand(time(NULL));
int x,y,mins=;
for(i=;i<;i++) //随机900000次基本能过,在不超时的前提下,随机次数越多越好
{
x=rand()%n;
y=rand()%n;
if(x==y) continue;
int temp = geths(x,y);
if(mins>temp) mins=temp;
}
printf("%d\n",mins);
}
return ;
}
/*
2
2
12345
54321
4
12345
6789A
BCDEF
0137F
*/

HDU 4217 Hamming Distance 随机化水过去的更多相关文章

  1. hdu 4712 Hamming Distance 随机

    Hamming Distance Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) ...

  2. hdu 4712 Hamming Distance ( 随机算法混过了 )

    Hamming Distance Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) ...

  3. HDU 4712 Hamming Distance(随机算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4712 解题报告:输入n个数,用十六进制的方式输入的,任意选择其中的两个数进行异或,求异或后的数用二进制 ...

  4. hdu 4712 Hamming Distance(随机函数暴力)

    http://acm.hdu.edu.cn/showproblem.php?pid=4712 Hamming Distance Time Limit: 6000/3000 MS (Java/Other ...

  5. HDU 472 Hamming Distance (随机数)

    Hamming Distance Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) To ...

  6. HDU 4712 Hamming Distance(随机算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4712 题目大意:任意两个数按位异或后二进制中含1的个数被称为海明距离,给定n个数,求出任意其中两个最小 ...

  7. Hamming Distance(随机算法)

    http://acm.hdu.edu.cn/showproblem.php?pid=4712 题意:计算任意两个十六进制的数异或后1的最少个数. 思路:用随机数随机产生两个数作为下标,记录这两个数异或 ...

  8. hdu 4712 Hamming Distance(随机数法)

    d.汉明距离是使用在数据传输差错控制编码里面的,汉明距离是一个概念,它表示两个(相同长度)字对应位不同的数量, 我们以d(x,y)表示两个字x,y之间的汉明距离.对两个字符串进行异或运算,并统计结果为 ...

  9. hdu 4712 Hamming Distance bfs

    我的做法,多次宽搜,因为后面的搜索扩展的节点会比较少,所以复杂度还是不需要太悲观的,然后加上一开始对答案的估计,用估计值来剪枝,就可以ac了. #include <iostream> #i ...

随机推荐

  1. Framebuffer 驱动学习总结(二)---- Framebuffer模块初始化

    ---恢复内容开始--- Framebuffer模块初始化过程:--driver\video\fbmem.c 1.  初始化Framebuffer: FrameBuffer驱动是以模块的形式注册到系统 ...

  2. Error: No resource found that matches the given name (at 'icon' with value '@mipmap/Icon')

    问题: error: Error: No resource found that matches the given name (at 'icon' with value '@mipmap/Icon' ...

  3. Runtime.getRuntime().exec 类 防止阻塞

    import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.IOException; impor ...

  4. 11 The Go Memory Model go语言内置模型

    The Go Memory Model go语言内置模型 Version of May 31, 2014 Introduction 介绍 Advice 建议 Happens Before 在发生之前 ...

  5. spring boot 中使用redis session

    spring boot 默认的httpsession是存在内存中.这种默认方式有几个缺点:1.当分布式部署时,存在session不一致的问题:2.当服务重启时session就会丢失,这时候用户就需要重 ...

  6. css文字环绕图片--遇到的问题及解决方法

    一.前言 需要实现一个文字环绕图片的效果,心想so easy嘛. 1)代码部分 <style> .img-left { border: 3px solid #005588; width:3 ...

  7. java IO流的继承体系和装饰类应用

    java IO流的设计是基于装饰者模式&适配模式,面对IO流庞大的包装类体系,核心是要抓住其功能所对应的装饰类. 装饰模式又名包装(Wrapper)模式.装饰模式以对客户端透明的方式扩展对象的 ...

  8. apachebench对网站进行并发测试

    ,安装apache ,打开cmd进入apache安装目录的bin目录(有ab.exe) ,执行ab命令 格式:ab -n -c http://localhost:80/test/test.php 说明 ...

  9. CI框架整合UEditor编辑器上传功能

    最近项目中要使用到富文本编辑器,选用了功能强大的UEditor,接下来就来讲讲UEditor编辑器的上传功能整合. 本文UEditor版本:ueditor1_4_3_utf8_php版本 第一步:部署 ...

  10. python开发学习-day14(jquery、ajax等)

    s12-20160421-day14 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: ...