标签:push var 相同 pre div let 删除 while log
//求最长相同子序列 function dp(s1,s2) { var n=s1.length,m=s2.length;//长度 var cs=‘‘; var n1=0,n2=0;//扫描点 var chunkArr=[] //开始扫描 while (n1<n&&n2<m){ //相等 if(s1[n1]===s2[n2]){ let nn1=n1+1; let nn2=n2+1; while (nn1<n&&nn2<m&&s1[nn1]===s2[nn2]){ nn1++; nn2++; } cs=cs+s1.substr(n1,nn1-n1) chunkArr.push([‘e‘,n1,nn1-n1]) n1=nn1; n2=nn2; }else{ let d1=0; let d2=0; if(n1+1<n){ d2=1; } if(n2+1<m){ d1=1; } if(d1===1&&d2===1){ if(s1[n1+1]!==s2[n2+1]){ while (n2+d1<m&&s1[n1]!==s2[n2+d1]){ d1++; } while (n1+d2<n&&s1[n1+d2]!==s2[n2]){ d2++; } } } if(d1===d2&&n-n1===m-n2){ //替换 n1=n1+1; n2=n2+1; }else if(d1<d2||n-n1<m-n2){ //增加 n2=n2+1; }else{ //删除 n1=n1+1; } } } return cs; } console.log(dp(‘252434343232‘,‘2234343‘))
输出:2234343
标签:push var 相同 pre div let 删除 while log
原文地址:https://www.cnblogs.com/caoke/p/12808213.html