HDU-4712-Hamming Distance(水随机数)

Hamming Distance

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


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
 


//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <algorithm>
#include <string>
#include <set>
#include <stack>
#include <time.h>
#include <map>
#include <queue>
#define INF 0x3f3f3f3f
using namespace std;
const int MAXN = 1e5+7;
const int MAXM = 1e4+7;
int tree[MAXN][2];
int pow16[10];
char str[10];
int a[25];
int temp[25];
int num[MAXN];
int main()
{
    int T;
    int n;
    pow16[0]=1;
    for(int i=1; i<7; ++i)
        pow16[i]=pow16[i-1]*16;
    cin>>T;
    while(T--)
    {
        // 12345   74565/
        //memset(tree,0,sizeof(tree));
        scanf("%d",&n);
       // root=new(node);
       // int flag=0;
        for(int i=0; i<n; ++i)
        {
            scanf("%s",str);
            long long p=0,ans=0;
            for(int j=0; j<5; ++j)
            {
                if(str[j]<='9'&&str[j]>='0')
                    p=str[j]-'0';
                else
                    p=str[j]-'A'+10;
                ans+=p*pow16[4-j];
            }
            num[i]=ans;
            //cout<<"ans:"<<ans<<endl;
        }
        //cout<<"ans:"<<ans<<endl;
        int Max=21;
        srand(time(NULL));
        for(int i=0; i<300006; ++i)
        {
            int shit=rand()%n;
            int fuck=rand()%n;
            if(shit==fuck)continue;
            int p=num[shit]^num[fuck];
            int ans=0;
            while(p)
            {
                ans+=p&1;
                p>>=1;
            }
            if(ans<Max)Max=ans;
        }
        //cout<<Max<<endl;
        printf("%d\n",Max);
           // in_sert(a);
    }
    return 0;
}







你可能感兴趣的:(C++,ACM,HDU)