http://acm.hdu.edu.cn/showproblem.php?pid=4712

Hamming Distance

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

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
 
【题解】:
  随机函数(没节操)暴力,(网上有大神这么水过的)
  一开始看到rand随机,就觉得真没节操,后来想了想,这题确实可以这么做,因为字符串长度只有5,所以答案肯定在0-20之间,而随机次数越多得到的答案就是最小值的机会也就越大,所以要尽量在保证不超时的前提下,增加尽量多的随机次数
 
【code】:
 #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 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 ( 随机算法混过了 )

    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 题目大意:任意两个数按位异或后二进制中含1的个数被称为海明距离,给定n个数,求出任意其中两个最小 ...

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

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

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

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

  6. hdu 4712 Hamming Distance bfs

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

  7. 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 ...

  8. HDU 472 Hamming Distance (随机数)

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

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

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

随机推荐

  1. discuze回放提示“抱歉,您的请求来路不正确或表单验证串不符,无法提交”

    不知从哪里看到文章,但是实用: 背景:discuze就单纯的录制一个注册脚本,日志中没有报错,在报告中就提示"抱歉,您的请求来路不正确或表单验证串不符,无法提交"",以下 ...

  2. 是时候全面使用html5标签了

    html5,这个词语,不管是业内还是业外,都已经耳熟能详了.因为已经火了这么长的的时间了.但是,真正开始使用的又有多少人呢?只能用呵呵来形容了! html5真的来了 2014年10月28日,历经八年, ...

  3. 第八章 jQuery与Ajax应用

    Ajax(Asynchronous JavaScript and XML),异步JavaScript和XML,它实现的无刷新更新页面,能够进行异步提交. jQuery对Ajax进行了封装,最底层的是$ ...

  4. Android Studio工程目录介绍

    来自知乎: Android Studio工程目录结构 .gradle 是gradle运行以后生成的缓存文件夹. .idea 是android studio/Intellij IDEA工程打开以后生成的 ...

  5. html5 canvas 圆形抽奖的实例

    年底没啥,抽空学习了canvas,写了个html5抽奖的代码,造了个轮子,有用的童鞋可以拿走. 其中,canvas.onclick触发抽奖行为,概率可以在core.lottery()函数上添加,美化也 ...

  6. sql server日期时间转字符串(转)

    一.sql server日期时间函数Sql Server中的日期与时间函数 1.  当前系统日期.时间     select getdate()  2. dateadd  在向指定日期加上一段时间的基 ...

  7. js 书写规范

    1.字符串用单引号 2.运算符号和变量之间用空格建立间距 3.书写插件时使用 'use strict'; 开头 4.方法如果是获取或者设置数据集合则使用动词如  getData,setData,eac ...

  8. Java 读写XML文件 API--org.dom4j

    om4j是一个Java的XML API,类似于jdom,用来读写XML文件的.dom4j是一个十分优秀的JavaXML API,具有性能优异.功能强大和极其易使用的特点,同时它也是一个开放源代码的软件 ...

  9. ZigBee HomeAutomation分析

    引用请注明出处!联系邮箱是huhao0126@163.com 本例程讲解,基于TI CC2530-2.5.1a中的HomeAutomation文件夹中的SampleLight和SampleSwitch ...

  10. vs工程链接出现error LNK2005...already defined

    今天使用vs2008编译工程无错误,链接过程,出现很多这样的错误: error LNK2005: "public: __thiscall std::basic_string<char, ...