Quantcast
Channel: CSDN博客推荐文章
Viewing all articles
Browse latest Browse all 35570

hdu 4712 Hamming distance

$
0
0


不知道正解是神马,,不过网上都是这种随机破法,,也只好随大流了。。


#include<cstdio>  
#include<cstring>  
#include<iostream>  
#include<ctime>  
#include<cstdlib>  
using namespace std;  
  
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()  
{  
    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 = 0x3f3f3f3f;  
        srand( (unsigned)time( NULL ) );  
        for(int i=1;i<=100000;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;  
}  


作者:MetalSeed 发表于2013-9-19 12:07:59 原文链接
阅读:103 评论:0 查看评论

Viewing all articles
Browse latest Browse all 35570

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>