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. 一个简单二叉树的C++实现(一)

    很久没有接触二叉树了,写这个当作练手,接下来会比较详细地实现二叉树的各个功能及应用. /* * BinaryTree.cpp * Author: Qiang Xiao * Time: 2015-07- ...

  2. support STL Viewer with WordPress On SAE

    由于SAE不支持本地代码目录写入, 我把WordPress的uploads路径改到了Storage中, 使用Domain来存放非代码资源. 这导致STL Viewer插件无法正常使用. 解决方法: 把 ...

  3. hdu 4782 Beautiful Soupz

    模拟.其实这题就是题目比较长而已...读完题目就差不多了.tag直接读就可以了,题目说了不用修改.然后整个题目就是让求text部分,严格按空格分开.注意每行前面空格个数. #include<al ...

  4. SQLserver查询数据类型为ntext是空或NULL值的方法

    --为空的值text ntext select * from lf_newsNg_utf where datalength(newsContentE)=0 or datalength(newsCont ...

  5. glib 文档 代码 索引 编译

    ./configure --prefix=/opt/glib-2.28.8 --enable-staticmakemake install linux下载 WIN32下载 代码索引 文档索引 GLib ...

  6. ios8.1上运行程序,程序界面只显示一部分

    在ios 9.1上运行程序没问题 但是在8.1上运行发现模拟器上只显示了程序的一小部分界面,没有显示完全. 结果发现由以下代码设置问题引起的 - (BOOL)application:(UIApplic ...

  7. winform控件记录

    winform: --------------------------------------------------- Button控件: 属性: BackColor: 背景颜色 Backgroun ...

  8. 2013移动APP界面设计趋势与设计理念

    用户是上帝,一切还得从应用说起.为此,国外著名的应用设计师Gannon Burgett结合自己多年实战经验归纳总结了2013年App设计发展的13大趋势.我们应该时刻记着这13点. 1.扁平化设计—— ...

  9. php下正则表达式整理

    一.正则表达式的历史背景 1,内容深厚的正则表达式 ^.+@.+\\..+$ 形式 字符串搜索与匹配的工具 2,应用范围 手机输入法 Windows文件搜索 linux 列出文件命令 网站用户注册,如 ...

  10. Appium 服务命令行参数

    Appium  可以直接通过命令行启动,同样支持命令行参数配置 参考: http://appium.io/slate/cn/master/ 使用方法 例如: appium  --shell 服务器标志 ...