hdu 4712Hamming Distance

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
 
 
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <ctime>
 6 #define N 100010
 7 #define inf 0x3f3f3f3f
 8 using namespace std;
 9 int num[20][20];
10 char ss[N][6];
11 int n;
12 
13 int cal(int x,int y)
14 {
15     int ans=0;
16     for(int i=0;i<5;i++)
17     {
18         int dd1,dd2;
19         if(ss[x][i]>='0' && ss[x][i]<='9')
20         {
21             dd1=ss[x][i]-'0';
22         }
23         else
24         {
25             dd1=ss[x][i]-'A'+10;
26         }
27         if(ss[y][i]>='0' && ss[y][i]<='9')
28         {
29             dd2=ss[y][i]-'0';
30         }
31         else
32         {
33             dd2=ss[y][i]-'A'+10;
34         }
35         ans+=num[dd1][dd2];
36     }
37     return ans;
38 }
39 void solve()
40 {
41     srand((unsigned)time(NULL));
42     int res=inf;
43     for(int i=0;i<1000000;i++)
44     {
45         int x=rand()%n;
46         int y=rand()%n;
47         if(x==y)
48         continue;
49         int ans=cal(x,y);
50         res=min(res,ans);
51     }
52     printf("%d\n",res);
53 }
54 int main()
55 {
56     for(int i=0;i<16;i++)
57     {
58         for(int j=0;j<16;j++)
59         {
60             int dd=i^j;
61             int len=0;
62             int cnt=0;
63             while(dd>=(1<<len))
64             {
65                 if(dd&(1<<len))
66                 {
67                     cnt++;
68                 }
69                 len++;
70             }
71             num[i][j]=cnt;
72         }
73     }
74     int t;
75     scanf("%d",&t);
76     while(t--)
77     {
78         scanf("%d",&n);
79         for(int i=0;i<n;i++)
80         {
81             scanf("%s",ss[i]);
82         }
83         solve();
84     }
85     return 0;
86 }
View Code

居然是用随机函数就给过了,真厉害,算是学到了一个好方法啊,学习学习

转载于:https://www.cnblogs.com/ouyangduoduo/p/3310829.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值