博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 4217 Hamming Distance 随机化水过去
阅读量:6211 次
发布时间:2019-06-21

本文共 2212 字,大约阅读时间需要 7 分钟。

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 
#include
#include
#include
#include
using namespace std;#define N 100000char str[N+10][10];int mark[20][20]; //make中存 i^j 的1的个数int arr[]={
0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4}; //0-F 中1的个数int charToHex(char ch) //将0-F字符转换成10进制数计算{ if(isdigit(ch)) return ch-'0'; return ch-'A'+10;}void getMark() //求mark数组{ int i,j,s; for(i=0;i<16;i++) { for(j=i;j<16;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=0; for(i=0;i<5;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=0;i
temp) mins=temp; } printf("%d\n",mins); } return 0;}/*2212345543214123456789ABCDEF0137F*/

 

转载地址:http://njsja.baihongyu.com/

你可能感兴趣的文章
Hadoop学习之路(五)Hadoop集群搭建模式和各模式问题
查看>>
为什么面试要问 hashmap 的原理
查看>>
numpy用法小结
查看>>
rinetd 一个linux下的端口转发工具
查看>>
Hibernate-validator校验框架使用
查看>>
最简单的基于FFmpeg的AVDevice例子(读取摄像头)
查看>>
jquery显示和隐藏元素
查看>>
Nginx+Tomcat高性能负载均衡集群搭建
查看>>
vivado2015.4 simulator 存储所有信号到 .wdb 文件 并打开波形文件查看波形
查看>>
0407-服务注册与发现-Eureka深入理解-元数据、高可用HA
查看>>
【转】MongoDB学习笔记(查询)
查看>>
stixel-world跑在kitti数据集
查看>>
pycharm+PyQt5+python最新开发环境配置
查看>>
keepalived VS zookeeper
查看>>
类锁与对象锁,重入锁
查看>>
StratifiedShuffleSplit 交叉验证
查看>>
MySQL时间增加、字符串拼接
查看>>
***ThinkPHP中的常用方法汇总总结:M方法,D方法,U方法,I方法
查看>>
vue当前路由跳转初步研究
查看>>
[MicroPython]TurniBit开发板DIY自动窗帘模拟系统
查看>>