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名万名考生(假设我的代码是正确的)。

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值