Hamming Distance

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

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
 


题目大意:给你很多串,最多10^5个串,每个串长度是5,16进制转化为2进制,问你任意两个串抑或得到1的最小的个数。


解题思路:
做题的时候只顾着如何降低复杂度,简单的O(n^2)肯定会超时,最后才得知是随机函数写。结果只可能是0~20.自己写的时候10W次还是WA了,人品不行啊,果断随机100W次A了。 不过听说可以更暴力地直接枚举最前面的10000个也可以A。网络赛要敢于尝试。

题目地址:Hamming Distance

AC代码:
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdio>
#include<algorithm>
using namespace std;
int a[100005]; int main()
{
int tes,i,j,k,res,ans;
scanf("%d",&tes);
while(tes--)
{
int n;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%X",&a[i]); //16进制读取 res=20; //结果初始为最大20
for(i=1;i<=1000000;i++)
{
j=rand()%n; //随机函数
k=rand()%n;
if(j==k)
continue;
ans=0;
int tmp=a[j]^a[k]; //抑或
while(tmp) //抑或算1的个数,保存到ans中
{
if(tmp&1)
ans++;
tmp>>=1;
}
if(ans<res)
res=ans;
}
cout<<res<<endl;
}
return 0;
} //2453MS 676K



HDU 4712Hamming Distance(随机函数运用)的更多相关文章

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

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

  2. HDU 5812 Distance

    从a变到b,也就是将a一直除素因子,除到1为止,然后乘b的素因子,一直乘到b. 但是gcd(a,b)部分是不用除下去的.所以d(a,b)=a/gcd(a,b)的素因子个数+b/gcd(a,b)的素因子 ...

  3. HDU 5102 The K-th Distance(模拟)

    题意:输入一棵树,输出前k小的点对最短距离dis(i,j)的和. 模拟,官方题解说得很清楚了.不重复了. http://bestcoder.hdu.edu.cn/ 需要注意的是,复杂度要O(n+k), ...

  4. HDU 4712:Hamming Distance

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

  5. hdu 4712 Hamming Distance 随机

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

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

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

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

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

  8. HDU 472 Hamming Distance (随机数)

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

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

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

随机推荐

  1. 工作流管理系统 jBPM

    工作流管理系统 jBPM 运行环境: 授权方式:BSD 软件大小:M 下载量:589 更新日期:2014-04-04 来源地址: 联系作者:Linux     jBpm是一个灵活可扩展的工作流管理系统 ...

  2. Android——用户登陆及用户名和密码的保存

    Android——用户登陆及用户名和密码的保存   在之前的学习过程中已经将Android学习完了,但是在后面将近一年的时间里都没有进行过Android开发,所以对Android的所有的知识点又有点忘 ...

  3. ASP.NET jQuery 随笔 显示CheckBoxList成员选中的内容

    通过jQuery来获取CheckBoxList成员内容. <%@ Page Language="C#" AutoEventWireup="true" Co ...

  4. ie条件注释还能这样写

    通过条件注释给html开始标签定义不同的class, 来区分不同版本的IE,可以在样式表中避免 样式属性hack (如 _margin-top, *float:none ) 注意: IE10+不支持条 ...

  5. STL assign 和swap

    首先看下在整个container上面的复制. c1=c2 可以等同于 c1.erase(c1.begin(),c1.end()) //delete all elems in c1 c1.insert( ...

  6. 2014ACM/ICPC亚洲区鞍山赛区现场赛1009Osu!

    鞍山的签到题,求两点之间的距离除以时间的最大值.直接暴力过的. A - Osu! Time Limit:1000MS     Memory Limit:262144KB     64bit IO Fo ...

  7. (3)选择元素——(3)$()方法(The $() function)

    No matter which type of selector we want to use in jQuery, we always start with the same function: $ ...

  8. 复习知识点:UITableView和UICollectionView的常用属性

    UITableView UICollectionView  //UICollectionViewLayout //UICollectionViewLayout决定了UICollectionView如何 ...

  9. Android:基于Eclipse编译调试系统级应用源代码

    一.      概要描述 在使用Eclipse导入android工程源代码以后,我们可以使用ddms调试和跟踪源代码. 本文讲述动态调试源代码和静态调试源代码的两种方法,避免build system. ...

  10. 【Nginx】启动报错-端口被占用

    将下载的windows版nginx的压缩包nginx-1.4.2.zip解压到F:\server\nginx-1.4.2里面. dos命令键入: F: cd F:\server\nginx-1.4.2 ...