Description
For 2 non-negative integers x and y, f(x, y) is defined as the number of different bits in the binary format of x and y. For example, f(2, 3)=1,f(0, 3)=2, f(5, 10)=4. Now given 2 sets of non-negative integers A and B, for each integer b in B, you should find an integer a in A such that f(a, b) is minimized. If there are more than one such integer in set A, choose the smallest one.
Input
The first line of the input is an integer T (0 < T ≤ 100), indicating the number of test cases. The first line of each test case contains 2 positive integers m and n (0 < m, n ≤ 100), indicating the numbers of integers of the 2 sets A and B, respectively. Then follow (m + n) lines, each of which contains a non-negative integers no larger than 1000000. The first m lines are the integers in set A and the other n lines are the integers in set B.
Output
For each test case you should output n lines, each of which contains the result for each query in a single line.
Sample Input
2 2 5 1 2 1 2 3 4 5 5 2 1000000 9999 1423 3421 0 13245 353
Sample Output
1 2 1 1 1 9999 0
题目大意:输入第一个数表示T组测试实例,接下来两个数m,n分别表示集合A、B元素的个数,接下来m+n行,是集合A、B的元素,求将他们都转化为二进制数以后,B集合里的每一个元素与A集合所有的元素比较不相同数的个数,输出不相同个数最小的对应A集合里面的元素。
哎,不知道为什么WA了,找不到错误,测试实例都对呀。
#include <iostream>
#include <vector>
#include <stdio.h>
#include <memory.h>
using namespace std;
#define N 205
vector <int> vec[N];
void Binary(int x,int i)
{
vec[i].clear();
if(x==0)
vec[i].push_back(x);
while(x!=0)
{
int y=x%2;
vec[i].push_back(y);
x/=2;
}
}
int main()
{
int t,n,m,a[N],s[N],z;
cin>>t;
while(t--)
{
cin>>n>>m;
for(int i=0; i<n+m; i++)
{
cin>>a[i];
Binary(a[i],i);
while(vec[i].size()<21)
vec[i].push_back(0);
}
for(int i=n; i<n+m; i++)
{
memset(s,0,sizeof(s));
int mim=1000000;
for(int j=0; j<n; j++)
{
for(int k=0; k<21; k++)
if(vec[i][k]!=vec[j][k])
s[j]++;
if(s[j]<mim)
{
mim=s[j];
z=j;
}
}
printf("%d\n",a[z]);
}
}
return 0;
}