hdu 4712Hamming Distance (厕所大号出的题解)随机函数~

Hamming Distance

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


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


比赛的时候本来想了很久都没思路,   结果和另外一个队友去厕所大号,  聊着聊着我就说,  其实这道题W个400次, 肯定可以过的。  因为才20组,  每组可能20个答案..


本来说笑而已,  结果我们突然发现, 只用生成一个随机函数, 随机选取两个数组的下标, 生成答案,  这样只要随机的次数够多就可以了。  


结果抱着一丝的希望提交,  没想到一交就A了。害羞


#include <cstdio>
#include <algorithm>
#include <string>
#include <cmath>
#include <set>
#include <map>
#include <vector>
#include <cstdlib>
#include <queue>
#include <iostream>
#include <functional>
#include <cstring>
#include <ctime>

#define UFOR(i, a, b) for(int i = a; i <= b; i++)
#define DFOR(i, a, b) for(int i = a; i >= b; i--)
#define MEM0(a) memset(a, 0, sizeof(a))
#define MEM1(a) memset(a, -1, sizeof(a))
#define MEMINF(a) memset(a, 0x3f3f3f3f, sizeof(a))
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define FIN freopen("in.txt", "r", stdin)
#define FOUT freopen("out.txt", "w", stdout)

using namespace std;

//typedef __int64 LL;
typedef long long LL;
const int MXN = 1e5 + 10;
const int MXM = 2e4 + 130;
const int HS = 1000007;
const int INF = 0x3f3f3f3f;
const int MOD = 1000000007;


int T, n;
int A[MXN];


int Get_Num(int m) {
    int ret = 0;
    while(m) {
        ret += (m & 1);
        m >>= 1;
    }
    return ret;
}


int main() {
    scanf("%d", &T);
    while(T --) {
        scanf("%d", &n);
        for(int i = 0; i < n; i ++) {
            scanf("%X", &A[i]);
        }
        srand(time(NULL));
        int  Min = INF;
        for(int i = 0; i < 1e6; i ++) {
            int a = rand() % n;
            int b = rand() % n;
            if(a == b) continue;
            Min = min(Min, Get_Num(A[a] ^ A[b]));
        }
        printf("%d\n",Min);
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用!有问题请及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载!欢迎交流学习!不清楚的可以私信问我! 毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值