JZOJ 3231. Hamming distance

topic

Description

For binary string a, b, the Hamming distance between them is the number of strings after two strings of XOR 1. XOR rule is:

0 the XOR 0 = 0

. 1. 1 the XOR 0 =

0 = the XOR. 1. 1

. 1. 1 = 0 the XOR

calculation Hamming distance between two strings, they must be the same length. Now we give the N different binary strings, calculate the shortest Hamming distance between the strings twenty-two.
 

Input

The first number is the integer T (T≤10), representative of the number of sets of data.

Then there are T set of data, the first line of each data is a positive integer N, the number representing different binary strings. Followed by N rows, each row is a binary string (length of 5). We use numbers (0-9) and character (AF) to represent the binary string. It represents the binary string of hexadecimal code. For example, "12345" represented by the binary string is "00010010001101000101." 

Output

For each data, output an integer value that is the answer.
 

Sample Input

2 
2
12345
54321
4
12345
6789A
BCDEF
0137F

Sample Output

6
7
 

Data Constraint

For 30% of the data have 1≤N≤100

for all data, there 1≤N≤100000

 

 

analysis

 

  • This question is just the beginning, I used to run fast row
  • No later found, called n ^ 2
  • The water then had a 50
  • Later, I direct 1-2 ^ 20 * n to run, found that the direct bombing
  • Correct solution:
  • We want to take advantage of the number 1 directly to the formation of several
  • Such a find can be output directly after the

 

 

Code

 1 #include<iostream>
 2 #include<cmath>
 3 #include<algorithm>
 4 #include<cstdio>
 5 #include<cstring>
 6 #define N 10000000
 7 #define ll long long 
 8 using namespace std;
 9 string s;
10 int a[N];
11 int n;
12 int h[N];
13 ll tran(string s)
14 {
15     ll i,a,b;
16     double k,h=0;
17     a=s.length();
18     b=a-1;
19     for (i=0;i<=a;i++)
20     {
21        if (s[i]<='9'&&s[i]>='0')
22          s[i]=s[i]-'0';
23        else if (s[i]<='F'&&s[i]>='A')
24           s[i]=s[i]-'A'+10;
25        k=s[i]*pow(16, b);
26        h+=k;
27        b--;
28     }
29     return h;
30 }
31 bool dfs(int x,int y,int l,int r)
32 {
33     if (l==r+1)
34     {
35         for (int i=1;i<=n;i++)
36             if (h[a[i]^x]==1) return 1;
37         return 0; 
38     } 
39     for (int i=y+1;i<=20;i++)
40     {
41         int m=(1<<i);
42         if (dfs(x+m,i,l+1,r)) return 1;
43     }
44     return 0;
45 }
46 int main ()
47 {
48     int T;
49     cin>>T;
50     while (T)
51     {
52         memset(h,0,sizeof(h));
53         T--;
54         cin>>n;
55         for (int i=1;i<=n;i++)
56         {
57             cin>>s;
58             int t=tran(s);
59             a[i]=t;
60             h[a[i]]=1;
61         }
62         int ans=1;
63         while (1)
64           if (dfs(0,-1,1,ans)) break;
65           else ans++;
66         cout<<ans<<endl;
67     }
68 }

 

 

Guess you like

Origin www.cnblogs.com/zjzjzj/p/11112078.html