/**
* 字符串相似度(编辑距离),比如用在DNA对比。
* http://www.cnblogs.com/huangxincheng/archive/2012/11/11/2765633.html
*
*/
const {cache,time} = require('./aop');
function minEdit(seqA,seqB){
let lenA = seqA.length;
let lenB = seqB.length;
if(lenA==0 || lenB == 0){
return lenA||lenB;
}
let subSeqA = seqA.substr(0,lenA-1);
let subSeqB = seqB.substr(0,lenB-1);
let tailA = seqA[lenA-1];
let tailB = seqB[lenB-1];
if(tailA == tailB){
return minEdit(subSeqA,subSeqB);
}
let m1 = minEdit(subSeqA,seqB);
let m2 = minEdit(subSeqA,subSeqB);
let m3 = minEdit(seqA,subSeqB);
return Math.min(m1,m2,m3)+1;
}
minEdit = cache(minEdit);
function main(){
let seqA = 'ABCDE';
let seqB = 'BC';
let res = minEdit(seqA,seqB);
console.info(res);
}
time(main)();
console.info(cache.cache);
cache.clear();
const costTime = time.costTime;
console.info(`耗时:${costTime}ms`);
字符串相似度(编辑距离)
最新推荐文章于 2024-06-14 09:50:51 发布