A-M baby
This commit is contained in:
parent
764da6cbbb
commit
db842d013d
19005 changed files with 197040 additions and 7 deletions
|
|
@ -0,0 +1,35 @@
|
|||
function lcs(x,y){
|
||||
var s,i,j,m,n,
|
||||
lcs=[],row=[],c=[],
|
||||
left,diag,latch;
|
||||
//make sure shorter string is the column string
|
||||
if(m<n){s=x;x=y;y=s;}
|
||||
m = x.length;
|
||||
n = y.length;
|
||||
//build the c-table
|
||||
for(j=0;j<n;row[j++]=0);
|
||||
for(i=0;i<m;i++){
|
||||
c[i] = row = row.slice();
|
||||
for(diag=0,j=0;j<n;j++,diag=latch){
|
||||
latch=row[j];
|
||||
if(x[i] == y[j]){row[j] = diag+1;}
|
||||
else{
|
||||
left = row[j-1]||0;
|
||||
if(left>row[j]){row[j] = left;}
|
||||
}
|
||||
}
|
||||
}
|
||||
i--,j--;
|
||||
//row[j] now contains the length of the lcs
|
||||
//recover the lcs from the table
|
||||
while(i>-1&&j>-1){
|
||||
switch(c[i][j]){
|
||||
default: j--;
|
||||
lcs.unshift(x[i]);
|
||||
case (i&&c[i-1][j]): i--;
|
||||
continue;
|
||||
case (j&&c[i][j-1]): j--;
|
||||
}
|
||||
}
|
||||
return lcs.join('');
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue