Recently went to several company interviews, there are some big companies (such as penguins) of the assessment content really retarded, test are some algorithms, ni, take a just graduated students to do may also be able to, it seems to have been not in the recruit server, for example, the penguin has been a number of departments have a problem: how to know the set A, The dependencies in B.
It means which elements are in the a,b and which elements do not appear in the a,b.
In fact, the solution is very simple, learned the algorithm can be done completely. We know that the set has the opposite sex, that is, the elements in the set can only appear once, you can build a map< key, value, is the standard library or built a red-black tree does not matter,
The elements in the A,b set as key, each traversal, inserted in the map, each insert value from one time, and finally traverse the map again, all value = 2 key is a,b Common, value=1 key is A,b dissimilarity.
If the elements in a, b are integers and are small, the problem is reduced to a similar sort of counting, and the solution is as follows:
a{2,4,6,10,7,25} b{3,9,4,2,19,25}
#define MAX_NUM
int Test (int a[], int a_len, int b[], int b_len)
{
int size, I, MAX, Tp_key;
int *tmp;
if (!a_len | |!b_len)
{
return-1;
}
size = max_num * sizeof (int);
TMP = malloc (size);
if (!tmp)
{
return-1;
}
memset (tmp, 0, size);
max = 0;
for (i = 0; i < A_len ++i)
{
tp_key = a[i];
max = tp_key > Max? Tp_key:max;
tmp[tp_key]++;
}
for (i = 0; i < B_len ++i)
{
tp_key = b[i];
max = tp_key > Max? Tp_key:max;
tmp[Tp_key] + +;
}
++max;
for (i = 0; i < max; ++i)
{
if (!tmp[i))
continue;
if (1 = = tmp[i] )
{
printf ("%d is in A or b\n", i);
}
else
{
printf ("%d is in A and b\n", I);
}
Free (TMP);
return 0;
}