HDU - 4712 Hamming Distance(坑爹的随机数算法 + 暴力求解)

Hamming Distance

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)

Total Submission(s): 1728    Accepted Submission(s): 680

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

我想对出题的人说一句话,坑爹的题目,坑爹随机算法



其中可能提交会出现错误,但是多提交几次就对了(建议大家该为1e6就可以了)
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <cstdio>
#include <string>
#include <ctime>
using namespace std;
typedef long long LL;
const int MAXN = 1e5 + 5;
const int INF = 0x3f3f3f3f;
int T, n;
int A[MAXN];
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 < 1e5; 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;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-12-13 19:33:50

HDU - 4712 Hamming Distance(坑爹的随机数算法 + 暴力求解)的相关文章

HDU 4712 Hamming Distance (随机函数)

Hamming Distance Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 1806    Accepted Submission(s): 714 Problem Description (From wikipedia) For binary strings a and b the Hamming distance is equal

hdu 4712 Hamming Distance 随机

Hamming Distance Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) 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 Hammi

hdu 4712 Hamming Distance(随机数法)

d.汉明距离是使用在数据传输差错控制编码里面的,汉明距离是一个概念,它表示两个(相同长度)字对应位不同的数量, 我们以d(x,y)表示两个字x,y之间的汉明距离.对两个字符串进行异或运算,并统计结果为1的个数,那么这个数就是汉明距离. 给出N个串,求出其中最小的汉明距离(其中某2个串的汉明距离是最小的). s.随机数法... c.能不能过,看脸... #include<iostream> #include<stdio.h> #include<string.h> #inc

hduoj 4712 Hamming Distance 2013 ACM/ICPC Asia Regional Online —— Warmup

http://acm.hdu.edu.cn/showproblem.php?pid=4712 Hamming Distance Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 1610    Accepted Submission(s): 630 Problem Description (From wikipedia) For bina

HDU 4217 Hamming Distance 随机化水过去

Hamming Distance Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 1569    Accepted Submission(s): 616 Problem Description (From wikipedia) For binary strings a and b the Hamming distance is equal

hdu 4712 随机数方法求解

#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<string> #include<vector> #include<time.h> using namespace std; const int Max = 100050; char str[Max][10]; int mark[16][16]; int nu

相似算法 ,Java实例9 - 汉明距离 Hamming Distance

Java实例9 - 汉明距离 Hamming Distance http://blog.csdn.net/kindterry/article/details/6581344 /**在信息理论中,两个等长字符串之间的汉明距离 * 是两个字符串对应位置上不同字符的个数, * 换句话说,汉明距离就是将一个字符串替换成另外一个字符串所需要替换的字符长度. *例如,1011101和1001001之间的汉明距离是2, *toned和roses之间的汉明距离是3. *汉明重量是字符串相对于同样长度的零字符串的

729 - The Hamming Distance Problem

// 题意: // 输入两个整数N, H,按照字典序输出所有长度为N,恰好包含H个1的01串 // 规模:1<=H<=N<=16 // 算法A:2^N枚举,输出1的个数为H的.采用递归枚举 // 从bits[d]开始确定,已经用了c0个0和c1个1 #include<cstdio> #include<cstring> #include<iostream> #include<string> #include<algorithm>

hdu 2586 LCA模板题(离线算法)

http://acm.hdu.edu.cn/showproblem.php?pid=2586 Problem Description There are n houses in the village and some bidirectional roads connecting them. Every day peole always like to ask like this "How far is it if I want to go from house A to house B&quo