java 汉明距离_通过Java中给定的最大汉明距离(失配数量)获取所有字符串组合...

只要使用普通置换生成算法,除了传递距离,当你有不同的字符时递减它。

static void permute(char[] arr, int pos, int distance, char[] candidates)

{

if (pos == arr.length)

{

System.out.println(new String(arr));

return;

}

// distance > 0 means we can change the current character,

// so go through the candidates

if (distance > 0)

{

char temp = arr[pos];

for (int i = 0; i < candidates.length; i++)

{

arr[pos] = candidates[i];

int distanceOffset = 0;

// different character, thus decrement distance

if (temp != arr[pos])

distanceOffset = -1;

permute(arr, pos+1, distance + distanceOffset, candidates);

}

arr[pos] = temp;

}

// otherwise just stick to the same character

else

permute(arr, pos+1, distance, candidates);

}

调用具有:

permute("AGCC".toCharArray(), 0, 1, "ACTG".toCharArray());

性能注:

为20的字符串长度,5距离和一个5个字母,已经有超过17名万名考生(假设我的代码是正确的)。

上面的代码在我的机器上花费不到一秒钟的时间(不打印),但不要期望任何生成器能够在合理的时间内生成更多的数据,因为有简直太多的可能性。


版权声明:本文为weixin_42361929原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。