zoukankan      html  css  js  c++  java
  • hdu 4712 Hamming Distance(随机函数暴力)

    http://acm.hdu.edu.cn/showproblem.php?pid=4712

    Hamming Distance

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

    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
     
    【题解】:
      随机函数(没节操)暴力,(网上有大神这么水过的)
      一开始看到rand随机,就觉得真没节操,后来想了想,这题确实可以这么做,因为字符串长度只有5,所以答案肯定在0-20之间,而随机次数越多得到的答案就是最小值的机会也就越大,所以要尽量在保证不超时的前提下,增加尽量多的随机次数
     
    【code】:
     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <algorithm>
     4 #include <string.h>
     5 #include <time.h>
     6 
     7 using namespace std;
     8 #define N 100000
     9 
    10 char str[N+10][10];
    11 int mark[20][20];  //make中存 i^j 的1的个数
    12 int arr[]={0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4};  //0-F 中1的个数
    13 
    14 int charToHex(char ch)  //将0-F字符转换成10进制数计算
    15 {
    16     if(isdigit(ch)) return ch-'0';
    17     return ch-'A'+10;
    18 }
    19 
    20 void getMark() //求mark数组
    21 {
    22     int i,j,s;
    23     for(i=0;i<16;i++)
    24     {
    25         for(j=i;j<16;j++)
    26         {
    27             s=i^j;
    28             mark[i][j]=mark[j][i]=arr[s];
    29         }
    30     }
    31 }
    32 
    33 int geths(int x,int y) //求x到y的Hamming distance
    34 {
    35     int i,sum=0;
    36     for(i=0;i<5;i++)
    37     {
    38         int xx = charToHex(str[x][i]);
    39         int yy = charToHex(str[y][i]);
    40         sum+=mark[xx][yy];
    41     }
    42     return sum;
    43 }
    44 
    45 int main()
    46 {
    47     int t;
    48     getMark();
    49     scanf("%d",&t);
    50     while(t--)
    51     {
    52         int n;
    53         scanf("%d",&n);
    54         int i;
    55         for(i=0;i<n;i++)
    56         {
    57             scanf("%s",str[i]);
    58         }
    59         srand(time(NULL));
    60         int x,y,mins=100;
    61         for(i=0;i<900000;i++) //随机900000次基本能过,在不超时的前提下,随机次数越多越好
    62         {
    63             x=rand()%n;
    64             y=rand()%n;
    65             if(x==y)    continue;
    66             int temp = geths(x,y);
    67             if(mins>temp)   mins=temp;
    68         }
    69         printf("%d
    ",mins);
    70     }
    71     return 0;
    72 }
    73 /*
    74 2
    75 2
    76 12345
    77 54321
    78 4
    79 12345
    80 6789A
    81 BCDEF
    82 0137F
    83 */
  • 相关阅读:
    安卓9.0内测的背后,是上万App开发者半年来的适配优化
    错误记录:vue跟vue编译器版本不一致
    jspdf简单使用
    vue input添加回车触发
    vue watch bug记录
    SecureCRT通过拷贝配置文件登陆
    仿射变换
    opencv图像的旋转
    图像旋转的原理
    CvScalar
  • 原文地址:https://www.cnblogs.com/crazyapple/p/3310859.html
Copyright © 2011-2022 走看看