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. 自动化中的PageObject思想

    谈着这个话题很大,因为我自己在自动化分层的方面做的就不是很好,最近在做java+selenium中,如果不分层去管理,代码就显得很乱,如下: 下面代码主要是做了一个登录,登录后在修改自己的一些个人信息 ...

  2. log4net在WinForm和ASP.net下的设置

    下载log4net.dll,放到bin目录下,然后引用到工程.下面说明配置和调用方法. 1.AssemblyInfo.cs末尾添加 [assembly: log4net.Config.XmlConfi ...

  3. 《JavaScript模式》读书笔记

    简介 在软件开发过程中,模式是指一个通用问题的解决方案.一个模式不仅仅是一个可以用来复制粘贴的代码解决方案,更多地是提供了一个更好的实践经验.有用的抽象化表示和解决一类问题的模板. 对象有两大类: 本 ...

  4. MyBatis(3.2.3) - Passing multiple input parameters

    MyBatis's mapped statements have the parameterType attribute to specify the type of input parameter. ...

  5. Ehcache(2.9.x) - API Developer Guide, Blocking and Self Populating Caches

    About Blocking and Self-Populating Caches The net.sf.ehcache.constructs package contains some applie ...

  6. Ajax B/S 聊天工具txt文件保存

    打算做一个两个或多个网页之间交流的功能,思路是多个页面聊天的内容存放到一个文件里,每个页面都有提交聊天功能,当提交聊天信息时保存到上面那个文件里, 在每个也页面里放一个定时器,每秒钟获取聊天文件里的记 ...

  7. Ubuntu系统中登陆阿里云服务器的方法

    如果您购买了阿里云服务器,恰巧又在使用Ubuntu操作系统,那么恭喜你来对地方了,今天给大家分享一下如何在Ubuntu中登陆阿里云服务器: 主要使用两款软件:1.SecureCRT:2.SecureF ...

  8. 第三十九篇、NavBar动态隐藏、设置透明、毛玻璃效果

    1.动态隐藏 - (void)viewDidLoad { [super viewDidLoad]; if ([self respondsToSelector:@selector(automatical ...

  9. JDK中工具类的使用

    JDK中内置了很多常用的工具类,且多以“s”结尾,如:集合工具类Collections,数组工具类Arrays,对象工具类Objects,文件工具类Files,路径工具类Paths,数学工具类Math ...

  10. 清理c盘垃圾(将一下代码复制到记事本然后把后缀名改为xxx.bat,然后双击,就ok了!!)

    @echo off echo 正在清除系统垃圾文件,请稍等...... del /f /s /q %systemdrive%\*.tmp del /f /s /q %systemdrive%\*._m ...