Hamming Distance

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

Total Submission(s): 1845    Accepted Submission(s): 740

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
 
Source
 

迷失在幽谷中的鸟儿,独自飞翔在这偌大的天地间,却不知自己该飞往何方……

题意:给出n个字符串,求每两个串相应位异或值的二进制中1的个数和的最小值!

例如:

12345

54321


一般的枚举会超时,因为我试过……,然后为了优化,给两个数字异或之后的值打表!然后,然后还是超时……
既然不能遍历每一种情况,那就随机吧!只要控制随机总次数,就可以控制时间长度,至于AC,那就靠运气啦!不过这样的随机总次数,一般都会AC……

#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <time.h>
using namespace std;
int  cmp[16][16]= {0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,1,0,2,1,2,1,3,2,2,1,3,2,3,2,4,3,1,2,0,1,2,3,1,2,2,3,1,2,3,4,2,3,2,1,1,0,3,2,2,1,3,2,2,1,4,3,3,2,1,2,2,3,0,1,1,2,2,3,3,4,1,2,2,3,2,1,3,2,1,0,2,1,3,2,4,3,2,1,3,2,2,3,1,2,1,2,0,1,3,4,2,3,2,3,1,2,3,2,2,1,2,1,1,0,4,3,3,2,3,2,2,1,1,2,2,3,2,3,3,4,0,1,1,2,1,2,2,3,2,1,3,2,3,2,4,3,1,0,2,1,2,1,3,2,2,3,1,2,3,4,2,3,1,2,0,1,2,3,1,2,3,2,2,1,4,3,3,2,2,1,1,0,3,2,2,1,2,3,3,4,1,2,2,3,1,2,2,3,0,1,1,2,3,2,4,3,2,1,3,2,2,1,3,2,1,0,2,1,3,4,2,3,2,3,1,2,2,3,1,2,1,2,0,1,4,3,3,2,3,2,2,1,3,2,2,1,2,1,1,0};;
char p[1000005][10];
int ddd(int q,int w)
{
    int a,b,s=0;
    for(int i=0; i<5; i++)
    {
        char x = p[q][i];
        char y = p[w][i];
        if(isdigit(x))a=x-'0';
        else a=x-'A'+10;
        if(isdigit(y))b=y-'0';
        else b=y-'A'+10;
        s += cmp[a][b];
    }
    return s;
}
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        int N;
        cin>>N;
        int minn=0xfffffff;
        for(int i=0; i<N; i++)
            cin>>p[i];
        srand((unsigned)time(NULL));
        for(int i=1; i<=100000; i++)
        {
            int a=rand()%N;
            int b=rand()%N;
            if(a==b)continue;
            int tmp=ddd(a,b);
            minn=tmp>minn?minn:tmp;
        }
        cout<<minn<<endl;
    }
    return 0;
}

HDU 4712:Hamming Distance的更多相关文章

  1. [LeetCode] Total Hamming Distance 全部汉明距离

    The Hamming distance between two integers is the number of positions at which the corresponding bits ...

  2. [LeetCode] 477. Total Hamming Distance 全部汉明距离

    The Hamming distance between two integers is the number of positions at which the corresponding bits ...

  3. 使用jax加速Hamming Distance的计算

    技术背景 一般认为Jax是谷歌为了取代TensorFlow而推出的一款全新的端到端可微的框架,但是Jax同时也集成了绝大部分的numpy函数,这就使得我们可以更加简便的从numpy的计算习惯中切换到G ...

  4. hdu 4712 Hamming Distance(随机函数暴力)

    http://acm.hdu.edu.cn/showproblem.php?pid=4712 Hamming Distance Time Limit: 6000/3000 MS (Java/Other ...

  5. hdu 4712 Hamming Distance 随机

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

  6. hdu 4712 Hamming Distance ( 随机算法混过了 )

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

  7. 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/Other ...

  8. HDU 472 Hamming Distance (随机数)

    Hamming Distance Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) To ...

  9. HDU 4217 Hamming Distance 随机化水过去

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

随机推荐

  1. ORA-16019: cannot use LOG_ARCHIVE_DEST_1 with LOG_ARCHIVE_DEST or LOG_ARCHIVE_DUPLEX_DEST

    用户反馈数据库设置归档后,无法启动,并报如下错误: SQL> startup ORA-: cannot use LOG_ARCHIVE_DEST_1 with LOG_ARCHIVE_DEST ...

  2. 对ASP.NET Cookie的一些新的认识

    做用户登录,我一直用form验证的方式.有时候,为了节省时间,用户希望用户名输入框能够记住用户名,省得下次重新输入.这个时候光用form验证是不行的,因为form验证的话,用户一退出系统就失效了,所以 ...

  3. Java基础之访问文件与目录——获取与文件存储有关的信息(GetFileStores)

    控制台程序,列出存储在系统中的文件的详细信息 import java.nio.file.FileStore; import java.nio.file.FileSystems; import java ...

  4. Leetcode: Nth Digit

    Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... Note: n i ...

  5. Lintcode: Singleton && Summary: Synchronization and OOD

    Singleton is a most widely used design pattern. If a class has and only has one instance at every mo ...

  6. ShowMessage和MessageDlg消息对话框(VCL)

    ShowMessage一个简单的消息提示: 例如:ShowMessage("xxxx"); MessageDlg(constAnsiString Msg, TMsgDlgType ...

  7. linux第6天 流协议-粘包

    今天学习的主要是对第5天的加强. 比如服务器的多进程,点对点应用聊天程序.父进程子进程互发消息.等等. 流协议-粘包 一般TCP协议会出现粘包,粘包产生的原因一般为.TCP协议是流式传输,不会根据用户 ...

  8. android 添加背景音乐

    MediaPlayer mediaPlayer=MediaPlayer.create(MainActivity.this,R.raw.qiji); mediaPlayer.start();

  9. java装饰者模式理解

    java 装饰者模式其实就是扩展子类的功能,和继承是一个性质. 但继承是在编译时就固定扩展了父类的一些功能,而装饰者模式是在运行过程中动态绑定对象,实现一个子类可以随时扩展功能. 将方法排列组合,也可 ...

  10. 取客户的银行帐号SQL

    SELECT ibybanks.bank_name, --银行 ibybanks.bank_branch_name, --分行 ibybanks.bank_account_num_electronic ...