zoukankan      html  css  js  c++  java
  • 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
    ",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
    ",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
    ",ans);
        }
        return 0;
    }


  • 相关阅读:
    工具类--map 转成xml xml转成map
    工具类--MD5Utils
    工具类--敏感信息掩码规则
    spring--Springmvc中@Autowired注解与@Resource注解的区别
    工具类--发送验证码短信
    工具类--日期工具类
    工具类--Excel 导出poi
    Jquery mobile中的 checkbox和radio的设置问题
    ASP和JS读写Cookie的问题
    js获取当前用户IP
  • 原文地址:https://www.cnblogs.com/suncoolcat/p/3320244.html
Copyright © 2011-2022 走看看