HDU4712+随机算法

随机算法

求n个20位的2进制串的MinDist。

Dist:两个串的异或结果中1的个数

 

/*

随机算法

*/

#include<algorithm>

#include<iostream>

#include<string.h>

#include<stdlib.h>

#include<stdio.h>

#include<math.h>

#include<queue>

#include<stack>

#include<time.h>

#include<map>

#include<set>

using namespace std;

typedef long long int64;

//typedef __int64 int64;

typedef pair<int64,int64> PII;

#define MP(a,b) make_pair((a),(b)) 

const int inf = 0x3f3f3f3f;

const double pi=acos(-1.0);

const int dx[]={1,-1,0,0};

const int dy[]={0,0,1,-1};

const double eps = 1e-8;

const int maxm = 100000+10;

const int maxn = 105;



char str[ maxm ][ 6 ];

int num1[ 22 ],num2[ 22 ];



int GetId( char aim ){

    int ans ;

    if( aim>='0'&&aim<='9' ) ans = aim - '0';

    else if( aim>='A'&&aim<='Z' ) ans = aim - 'A' + 10;

    return ans;

}



int solve( int x,int y ){

    int cnt = 0;

    int cc ;

    for( int i=0;i<5;i++ ){

        int aim = GetId( str[x][i] );

        cc = 4;

        while( cc ){

            num1[ cnt++ ] = aim%2;

            aim /= 2;

            cc -- ;

        }

    }

    //printf("cnt = %d\n",cnt);

    cnt = 0;

    for( int i=0;i<5;i++ ){

        int aim = GetId( str[y][i] );

        cc = 4;

        while( cc ){

            num2[ cnt++ ] = aim%2;

            aim /= 2;

            cc -- ;

        }

    }

    //printf("cnt = %d\n",cnt);

    cnt = 0;

    for( int i=0;i<20;i++ ){

        if( num1[i]!=num2[i] )

            cnt ++ ;

    }

    return cnt ;

}

    

int main(){

    int T;

    scanf("%d",&T);

    while( T-- ){

        int n;

        scanf("%d",&n);

        for( int i=0;i<n;i++ )

            scanf("%s",str[i]);

        srand( time(NULL) );

        int ans = inf;

        int x,y;

        for( int i=0;i<180000;i++ ){

            x = rand()%n;

            y = rand()%n;

            if( x==y ) continue;

            int temp = solve( x,y );

            if( temp<ans )

                ans = temp;

        }

        printf("%d\n",ans);

    }

    return 0;

}


 

 

你可能感兴趣的:(HDU)