zoukankan      html  css  js  c++  java
  • HDU4712Hamming Distance随机化算法

    Hamming Distance

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

    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
     
    Recommend
    liuyiding
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <cstdlib>
    #include <algorithm>
    #include <vector>
    #include <stack>
    #include <queue>
    #include <string>
    #include <set>
    #include <sstream>
    #include <map>
    #include <bitset>
    #include <ctime>
    using namespace std ;
    #define zero {0}
    #define INF 2000000000
    #define eps 1e-6
    typedef long long LL;
    int cmp[16][16];
    char data[1000005][6];
    int fun(int q,int w)
    {
        int a,b;
        int ret=0;
        for(int i=0;i<5;i++)
        {
            char x=data[q][i];
            char y=data[w][i];
            if(x>='0'&&x<='9')
                a=x-'0';
            else
                a=x-'A'+10;
            if(y>='0'&&y<='9')
                b=y-'0';
            else
                b=y-'A'+10;
    
            ret+=cmp[a][b];
        }
        return ret;
    }
    
    int main()
    {
        #ifdef DeBUG
            freopen("C:\Users\Sky\Desktop\1.in","r",stdin);
        #endif
        for(int i=0;i<=15;i++)
        {
            for(int j=0;j<=15;j++)
            {
                int ans=0;
                int tmp=i^j;
                for(int k=0;k<4;k++)
                {
                    if((1<<k)&tmp)
                        ans++;
                }
                cmp[i][j]=ans;
            }
        }
        int n;
        int t;
        scanf("%d",&t);
        while(t--)
        {
            scanf("%d",&n);
            for(int i=1;i<=n;i++)
                scanf("%s",data[i]);
            int ans=0x3f3f3f;
            srand((unsigned)time(NULL));
            for(int i=1;i<=300000;i++)
            {
                int a=rand()%n+1;
                int b=rand()%n+1;
                if(a==b)
                    b=b%n+1;
                int tmp=fun(a,b);
                if(tmp<ans)
                    ans=tmp;
            }
            cout<<ans<<endl;
        }
        
        return 0;
    }
    View Code
  • 相关阅读:
    Java实现 蓝桥杯VIP 算法训练 连接字符串
    大多云盘都挤兑在了企业级市场
    115能做到100个亿(2016年05月31日),2013营收两亿元
    Dropbox 有哪些鲜为人知的使用技巧?
    Qt 添加外部库文件(四种方法)
    百度全新的ARM架构服务器,一个2U机箱装6台,每台4个3T硬盘,每个机箱共72TB
    EntityFramework中支持BulkInsert扩展
    基于A2DFramework的事件机制实现
    事件机制与消息机制的架构设计区别
    .net平台下socket异步通讯(代码实例)
  • 原文地址:https://www.cnblogs.com/Skyxj/p/3310270.html
Copyright © 2011-2022 走看看