Hamming Distance

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

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
 
这道题的正常做法是O(n^2)的,很显然这个做法会大大方方的T掉
但是我们可以用随机化,然后悄悄不说话的水过去= =
#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 4217 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 解题报告:输入n个数,用十六进制的方式输入的,任意选择其中的两个数进行异或,求异或后的数用二进制 ...

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

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

  5. HDU 472 Hamming Distance (随机数)

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

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

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

  7. Hamming Distance(随机算法)

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

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

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

  9. hdu 4712 Hamming Distance bfs

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

随机推荐

  1. SPOJ 16549 - QTREE6 - Query on a tree VI 「一种维护树上颜色连通块的操作」

    题意 有操作 $0$ $u$:询问有多少个节点 $v$ 满足路径 $u$ 到 $v$ 上所有节点(包括)都拥有相同的颜色$1$ $u$:翻转 $u$ 的颜色 题解 直接用一个 $LCT$ 去暴力删边连 ...

  2. SLF4J multiple

    "C:\Program Files\Java\jdk1.8.0_65\bin\java" -Didea.launcher.port=7537 "-Didea.launch ...

  3. JsRender 前端渲染模板常用API学习

    JsRender 常用API 1. $.templates() $.templates()方法是用来注册或编译模板的,使用的情况有以下几种. 把html字符串编译编译成模板 获取使用script标签声 ...

  4. 【鬼脸原创】谷歌扩展--知乎V2.0

    目的: 用键盘替代鼠标,做一个安静刷知乎的美男(女)子! 功能:   功能 按键 说明 直接定位到搜索框 q   打开 首页 w   打开 话题 e   打开 发现 r   打开 消息 m   打开 ...

  5. 监控SQLServer作业执行情况脚本

    SELECT [sJOB].[job_id] AS [作业ID] , [sJOB].[name] AS [作业名] , CASE WHEN [sJOBH].[run_date] IS NULL OR ...

  6. test.c

    test.c #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include < ...

  7. 20155225 实验四《Android程序设计》实验报告

    20155225 实验四<Android程序设计>实验报告 一.安装Android Stuidio 问题一:安装完成后,打开提示我找不到SDK,我已经设置了环境变量,关掉提示,没有影响. ...

  8. 浏览器被hao123,hao524劫持的解决办法

    今天研究(翻,墙),装了几个插件,什么云帆.外遇.蓝灯 后来我的google浏览器被hao123劫持,百度浏览器被hao524劫持 删除浏览器快捷方式.属性目标里的后缀,过不多久又被劫持,把我搞毛了 ...

  9. Elasticsearch安全问题

    本节内容: 背景 修改默认的 Elasticsearch 集群名称 不要暴露 Elasticsearch 在公网上 不要以 root 身份运行 Elasticsearch 定期对 Elasticsea ...

  10. 微信小程序实现左滑删除源码

    左滑删除效果在app的交互方式中十分流行,比如全民应用微信 微信左滑删除 再比如曾引起很大反响的效率app Clear Clear左滑删除 从技术上来说,实现这个效果并不困难,响应一下滑动操作,移动一 ...