HDU 4712 Hamming Distance

Hamming Distance

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


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
 

分析:

看了半天没有读懂题意,好尴尬,不知道说了多少遍要好好学英语,可总是半途而废,坚持不下去。

也许是受的打击太小了,没有那种恒心与毅力,废话不多了。

网上基本都是随机的方法,不只是哪位大牛想到的,膜拜ing。

虽然读书少的我并不相信随机了20w次就AC了,当然这是事实,好方法,受教了!

代码:

#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
int a[1000005];
int main()
{
    int t, n;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d", &n);
        for(int i = 0; i < n; i++) scanf("%X", &a[i]);  //以16进制读入
        int IM = 20;                    //1 的最大个数为20
        for(int i = 0; i < 200000; i++) //20w次就过了
        {
            int j = rand() % n;     //随机数
            int k = rand() % n;
            if(j == k) continue;
            int cnt = 0;
            int tmp = a[j] ^ a[k];  //异或
            while(tmp)
            {
                if(tmp & 1) cnt++;  //求异或中 1 的个数
                tmp >>= 1;          //二进制中的右移,相当于tmp = tmp / 2
            }
            if(cnt < IM) IM = cnt;
        }
        printf("%d\n", IM);
    }
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值