33,010
社区成员
发帖
与我相关
我的任务
分享
public static int[] same5count(int[][] A, int[][] B)
{
int[] value = new int[A.Length];
#region B生成HASH
int index;
uint bitB, hashB;
Dictionary<uint, int> count = new Dictionary<uint, int>(B.Length * 6);
foreach (int[] b in B)
{
for (bitB = 0, index = b.Length - 1; index >= 0; index--) bitB |= 1U << (b[index] - 1);
for (index = b.Length - 1; index >= 0; index--)
{
if (count.ContainsKey(hashB = bitB ^ (1U << (b[index] - 1)))) count[hashB]++;
else count.Add(hashB, 1);
}
}
#endregion
#region 统计
int[] a;
uint bitA, hashA;
for (int i = A.Length - 1; i >= 0; i--)
{
for (a = A[i], bitA = 0, index = a.Length - 1; index >= 0; index--) bitA |= 1U << (a[index] - 1);
for (index = a.Length - 1; index >= 0; index--)
{
if (count.ContainsKey(hashA = bitA ^ (1U << (a[index] - 1)))) value[i] += count[hashA];
}
}
#endregion
return value;
}