hdu 4712 Hamming Distance(随机算法,4级)

发布时间:2016-12-9 13:59:25 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"hdu 4712 Hamming Distance(随机算法,4级)",主要涉及到hdu 4712 Hamming Distance(随机算法,4级)方面的内容,对于hdu 4712 Hamming Distance(随机算法,4级)感兴趣的同学可以参考一下。

Hamming Distance Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 892    Accepted Submission(s): 322 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 2013 ACM/ICPC Asia Regional Online —— Warmup   Recommend liuyiding 思路:随机算法 #include<cstdio> #include<iostream> #include<cstring> #include<ctime> #include<cstdlib> using namespace std; const int mm=1e5+9; int f[mm]; int get(int x) { int ret=0; while(x) { ++ret;x=x&(x-1); } return ret; } int main() { int cas,n,test; while(~scanf("%d",&cas)) while(cas--) { scanf("%d",&n); for(int i=0;i<n;++i) scanf("%x",&f[i]); test=mm; srand((int)time(0)); int a,b,ans=999; while(test--) { a=rand()%n; b=rand()%n; if(a==b)continue; ans=min(ans,get(f[a]^f[b])); } printf("%d\n",ans); } }

上一篇:“万能数据库查询分析器”5.04 发布,撰写的相关技术文章达63篇
下一篇:C#winform界面中如何显示当前时间,是用lable控件实现

相关文章

相关评论