码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 4712 Hamming Distance (随机函数)

时间:2015-08-19 23:29:36      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:

Hamming Distance

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


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
 
题意:N个十六进制数,将两个十六进制数转换成二进制数串后逐位异或,求最后得到的二进制串中含有1的最少的个数。
分析:其实这道想了很久就是不知道怎么去优化,因为数据也有10W,然而最后竟然告诉我是用随机数,想想最多也就20个串,每次随机选两个,得到正确答案的几率还是很大的,但不是100%,觉得应该取随机数的次数越大越能保证答案的正确性。
技术分享
#pragma comprint(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<string>
#include<iostream>
#include<cstring>
#include<cmath>
#include<stack>
#include<queue>
#include<vector>
#include<map>
#include<stdlib.h>
#include<time.h>
#include<algorithm>
#define LL __int64
#define FIN freopen("in.txt","r",stdin)
using namespace std;
const int MAXN=100000+5;
const int MAX=1<<21;
const int INF=0x3f3f3f3f;
int a[MAX+5],num[MAXN];
int kase,n;
void init()
{
    for(int i=0;i<=MAX;i++)
    {
        int cnt=0;
        for(int j=0;j<21;j++)
            if(i & (1<<j))
                cnt++;
        a[i]=cnt;
    }
}
int main()
{
    init();
    scanf("%d",&kase);
    while(kase--)
    {
        scanf("%d",&n);
        for(int i=0;i<n;i++)
            scanf("%x",&num[i]);
        int ans=INF;
        srand( (unsigned)time( NULL ) );
        for(int i=0;i<900000;i++)
        {
            int x=rand()%n;
            int y=rand()%n;
            if(x==y) continue;
            ans=min(ans,a[num[x]^num[y]]);
        }
        printf("%d\n",ans);
    }
    return 0;
}
View Code

 

HDU 4712 Hamming Distance (随机函数)

标签:

原文地址:http://www.cnblogs.com/clliff/p/4743521.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!