Hamming Distance

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 499    Accepted Submission(s): 163
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
 

ps:不知道正解是怎样做,用随机算法水过了。
思路:
答案的范围不大,0~20,数据不强,可以考虑随机算法。

代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <algorithm>
#define maxn 105
#define maxx 100005
#define INF 0x3f3f3f3f
using namespace std; int n,m,ans;
int xr[maxn][maxn];
int a[]= {0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4};
char s[maxx][6]; int cal(int k1,int k2)
{
int i,j,t=0,x,y;
for(i=0; i<5; i++)
{
x=s[k1][i];
if(x>='A'&&x<='F') x=x-'A'+10;
else x=x-'0';
y=s[k2][i];
if(y>='A'&&y<='F') y=y-'A'+10;
else y=y-'0';
t+=xr[x][y];
}
return t;
}
void solve()
{
int i,j,t,x,y;
srand((unsigned)time(NULL));
for(i=1; i<=100000; i++)
{
x=rand()%n;
y=rand()%n;
if(x==y) continue ;
t=cal(x,y);
if(ans>t) ans=t;
if(!ans) return ;
}
}
int main()
{
int i,j,t;
for(i=0; i<16; i++)
{
for(j=0; j<16; j++)
{
xr[i][j]=a[i^j];
}
}
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i=0; i<n; i++)
{
scanf("%s",s[i]);
}
ans=INF;
solve();
printf("%d\n",ans);
}
return 0;
}


hdu 4712 Hamming Distance ( 随机算法混过了 )的更多相关文章

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

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

  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 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. Hamming Distance(随机算法)

    http://acm.hdu.edu.cn/showproblem.php?pid=4712 题意:计算任意两个十六进制的数异或后1的最少个数. 思路:用随机数随机产生两个数作为下标,记录这两个数异或 ...

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

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

  8. hdu 4712 Hamming Distance bfs

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

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

随机推荐

  1. 关于 require的缓存

    有两个文件 a.js内容如下: var add = require("./t.js").add; var add2 = require("./t.js").ad ...

  2. PHP MySQL Select 之Select

    从数据库表中选取数据 SELECT 语句用于从数据库中选取数据. 语法 SELECT column_name(s) FROM table_name 注释:SQL 语句对大小写不敏感.SELECT 与 ...

  3. poj 3301 Texas Trip(几何+三分)

    Description After a day trip with his friend Dick, Harry noticed a strange pattern of tiny holes in ...

  4. Python的字符串与数字

    Python3.0通过“input”实现读取控制台的输入与用户实现交互.值得注意的是input接受的所有数据都是字符串,即使输入的是数字,依然会被当作字符串来处理.这就会出现一些问题,所以需要进行类型 ...

  5. WinForm RDLC SubReport Step by step

    最近在做的一个PO管理系统,因为要用到订单打印,没有用水晶报表,直接使用VS2010的Reporting.参考了网上的一些文章,但因为找到的数据是用于WebForm的,适配到WinForm有点区别,竟 ...

  6. WPF实现窗体最小化后小图标在右边任务栏下

    一 基本功能 1. 这里是用 NotifyIcon 控件来实现,但 WPF 下没有 NotifyIcon  控件,怎么办,用 WinForm 下的呗. 先引用  .NET 自带的两个程序集 Syste ...

  7. C#中对输出格式的初始化

    一.在输出的时候,\t和8个空格是不一样的,\t是跳转到下一个水平制表符,如果你在第一个水平制表符中写有数据123,那么跳转后跳转到9的位置上,中间只有5个空格,但是如果用8个空格来做分割的话,就会有 ...

  8. 登陆权限验证Session和Cookie用法及BasePage类使用

    最近在做ASP.NET的项目时,接触到了登陆权限模块,所有总结了一下登陆时用到的知识和方法技巧. 如图说明:实现的效果如图,由于验证码验证比较简单这里就不介绍了 首先用代码生成器生成项目,以三层为例进 ...

  9. c++中各种数据类型所占字节

    求各种数据类型所占用的字节数可调用sizeof函数,求各种数据类型的最大值可以调用limits标准库中的numeric_limits<T>::max(),numeric_limits< ...

  10. S - stl 的mapⅠ

    先来介绍一下stl中的map这个功能 头文件#include<map> map是STL的一个关联容器,它提供一对一的数据处理能力 就像一个人对应一个编号一样 定义 为  map<in ...