Hamming Distance

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

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
 

思路:涨知识,还可以随机;

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
const int N=1e5+,M=4e6+,inf=1e9+,mod=1e9+;
const ll INF=1e18+;
int getnum(char a)
{
if(a>=''&&a<='')
return a-'';
return a-'A'+;
}
int check(int x,int y)
{
int sum=;
while(x||y)
{
if(x%!=y%)
sum++;
x>>=;
y>>=;
}
return sum;
}
char ch[N][];
int mp[][];
int main(){
for(int i=;i<;i++)
{
for(int t=;t<;t++)
mp[i][t]=check(i,t);
}
int T;
scanf("%d",&T);
while(T--)
{
int x;
scanf("%d",&x);
for(int i=;i<x;i++)
scanf("%s",ch[i]);
int ans=inf;
for(int i=;i<=;i++)
{
int k=;
int n=rand()%x;
int m=rand()%x;
if(n==m)continue;
for(int t=;t<;t++)
k+=mp[getnum(ch[n][t])][getnum(ch[m][t])];
ans=min(ans,k);
}
printf("%d\n",ans);
}
return ;
}

hdu 4712 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(随机算法)

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

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

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

  4. HDU 4217 Hamming Distance 随机化水过去

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

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

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

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

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

  7. hdu 4712 Hamming Distance bfs

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

  8. hduoj 4712 Hamming Distance 2013 ACM/ICPC Asia Regional Online —— Warmup

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

  9. HDU 472 Hamming Distance (随机数)

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

随机推荐

  1. scala构建类似java的pojo

    主要看以下代码: package com.test.scalaw.test.demo import scala.beans.BeanProperty /** * scala构建类似java 的pojo ...

  2. Linux驱动开发 -- 打开dev_dbg() 【转】

    转自:http://blog.chinaunix.net/uid-20672559-id-3383042.html linux设备驱动调试,我们在内核中看到内核使用dev_dbg来控制输出信息,这个函 ...

  3. Spring的beans标签下可以有其他标签

    以前有对xsd(也就是schema文件)小做研究,有个小困惑,就是我们定义的元素只能使用定义的哪一些标签,比如<beans>下面就只能有自定义的哪一些,那为什么在引入<context ...

  4. linux系统修改CST时区

    ================================================= [root@tzyyserveryg ~]# date -R Fri, 28 Nov 2014 08 ...

  5. ftp -i -n -v <<! 其中 -n禁止自动登录到初始连接

    <<!说明是输入.如是结束了需要再输入 !例如:ftp  -i -n -v  <<! 这里的叹号代表是ftp 命令的开始get  文件exit  !      代表ftp的命令 ...

  6. 使用ResourceBundle访问资源文件(properties)帮助类

    import java.util.ResourceBundle; /** * 读取properties文件的帮助类 * @author */ public class PropertiesUtil { ...

  7. css不同浏览器兼容性调试 --- 转自: [http://wo.115.com/?ct=detail&id=31733&bid=1018841]

    css不同浏览器兼容性调试 IE6.0,IE7.0与Firefox的CSS兼容性问题1.DOCTYPE 影响 CSS 处理 2.FF: div 设置 margin-left, margin-right ...

  8. Animation Play/Stop测试

    测试结果: 1.当切换不同动画播放,可以直接调用Play或者CrossFade.会立即切过去 2.当相同动画再次播放,又想打断重头再播,需要先调用Stop. anim.Play("Test1 ...

  9. 提高 Linux 上 socket 性能

      http://www.cnblogs.com/luxf/archive/2010/06/13/1757662.html 基于Linux的Socket网络编程的性能优化   1 引言    随着In ...

  10. Linux内存模型

    http://blog.csdn.net/sunyubo458/article/details/6090946 了解linux的内存模型,或许不能让你大幅度提高编程能力,但是作为一个基本知识点应该熟悉 ...