zoukankan      html  css  js  c++  java
  • 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
    ",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

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

  • 相关阅读:
    对象池使用时要注意几点
    Flash3D学习计划(一)——3D渲染的一般管线流程
    714. Best Time to Buy and Sell Stock with Transaction Fee
    712. Minimum ASCII Delete Sum for Two Strings
    647. Palindromic Substrings(马拉车算法)
    413. Arithmetic Slices
    877. Stone Game
    338. Counting Bits
    303. Range Sum Query
    198. House Robber
  • 原文地址:https://www.cnblogs.com/ouyangduoduo/p/3310829.html
Copyright © 2011-2022 走看看