Another update from ingydotnet^djgoku
This commit is contained in:
parent
91df62d461
commit
948b86eafa
7604 changed files with 108452 additions and 22726 deletions
|
|
@ -1,11 +1,11 @@
|
|||
function lcs(a, b) {
|
||||
var aSub = a.substr(0, a.length-1);
|
||||
var bSub = b.substr(0, b.length-1);
|
||||
var aSub = a.substr(0, a.length - 1);
|
||||
var bSub = b.substr(0, b.length - 1);
|
||||
|
||||
if (a.length == 0 || b.length == 0) {
|
||||
return "";
|
||||
} else if (a.charAt(a.length-1) == b.charAt(b.length-1)) {
|
||||
return lcs(aSub, bSub) + a.charAt(a.length-1);
|
||||
if (a.length === 0 || b.length === 0) {
|
||||
return '';
|
||||
} else if (a.charAt(a.length - 1) === b.charAt(b.length - 1)) {
|
||||
return lcs(aSub, bSub) + a.charAt(a.length - 1);
|
||||
} else {
|
||||
var x = lcs(a, bSub);
|
||||
var y = lcs(aSub, b);
|
||||
|
|
|
|||
|
|
@ -1,35 +1,13 @@
|
|||
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('');
|
||||
}
|
||||
var longest = (xs, ys) => (xs.length > ys.length) ? xs : ys;
|
||||
|
||||
var lcs = (xx, yy) => {
|
||||
if (!xx.length || !yy.length) { return ''; }
|
||||
|
||||
var x = xx[0],
|
||||
y = yy[0];
|
||||
xs = xx.slice(1);
|
||||
ys = yy.slice(1);
|
||||
|
||||
return (x === y) ? lcs(xs, ys) :
|
||||
longest(lcs(xx, ys), lcs(xs, yy));
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,15 +1,35 @@
|
|||
var t=i;
|
||||
while(i>-1&&j>-1){
|
||||
switch(c[i][j]){
|
||||
default:i--,j--;
|
||||
continue;
|
||||
case (i&&c[i-1][j]):
|
||||
if(t!==i){lcs.unshift(x.substring(i+1,t+1));}
|
||||
t=--i;
|
||||
continue;
|
||||
case (j&&c[i][j-1]): j--;
|
||||
if(t!==i){lcs.unshift(x.substring(i+1,t+1));}
|
||||
t=i;
|
||||
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;}
|
||||
}
|
||||
}
|
||||
}
|
||||
if(t!==i){lcs.unshift(x.substring(i+1,t+1));}
|
||||
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('');
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,27 +1,15 @@
|
|||
function lcs_greedy(x,y){
|
||||
var symbols = {},
|
||||
r=0,p=0,p1,L=0,idx,
|
||||
m=x.length,n=y.length,
|
||||
S = new Buffer(m<n?n:m);
|
||||
p1 = popsym(0);
|
||||
for(i=0;i < m;i++){
|
||||
p = (r===p)?p1:popsym(i);
|
||||
p1 = popsym(i+1);
|
||||
idx=(p > p1)?(i++,p1):p;
|
||||
if(idx===n){p=popsym(i);}
|
||||
else{
|
||||
r=idx;
|
||||
S[L++]=x.charCodeAt(i);
|
||||
var t=i;
|
||||
while(i>-1&&j>-1){
|
||||
switch(c[i][j]){
|
||||
default:i--,j--;
|
||||
continue;
|
||||
case (i&&c[i-1][j]):
|
||||
if(t!==i){lcs.unshift(x.substring(i+1,t+1));}
|
||||
t=--i;
|
||||
continue;
|
||||
case (j&&c[i][j-1]): j--;
|
||||
if(t!==i){lcs.unshift(x.substring(i+1,t+1));}
|
||||
t=i;
|
||||
}
|
||||
}
|
||||
return S.toString('utf8',0,L);
|
||||
|
||||
function popsym(index){
|
||||
var s = x[index],
|
||||
pos = symbols[s]+1;
|
||||
pos = y.indexOf(s,pos>r?pos:r);
|
||||
if(pos===-1){pos=n;}
|
||||
symbols[s]=pos;
|
||||
return pos;
|
||||
}
|
||||
}
|
||||
if(t!==i){lcs.unshift(x.substring(i+1,t+1));}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,42 @@
|
|||
function lcs_greedy(x,y){
|
||||
var p1, i, idx,
|
||||
symbols = {},
|
||||
r = 0,
|
||||
p = 0,
|
||||
l = 0,
|
||||
m = x.length,
|
||||
n = y.length,
|
||||
s = new Buffer((m < n) ? n : m);
|
||||
|
||||
p1 = popsym(0);
|
||||
|
||||
for (i = 0; i < m; i++) {
|
||||
p = (r === p) ? p1 : popsym(i);
|
||||
p1 = popsym(i + 1);
|
||||
if (p > p1) {
|
||||
i += 1;
|
||||
idx = p1;
|
||||
} else {
|
||||
idx = p;
|
||||
}
|
||||
|
||||
if (idx === n) {
|
||||
p = popsym(i);
|
||||
} else {
|
||||
r = idx;
|
||||
s[l] = x.charCodeAt(i);
|
||||
l += 1;
|
||||
}
|
||||
}
|
||||
return s.toString('utf8', 0, l);
|
||||
|
||||
function popsym(index) {
|
||||
var s = x[index],
|
||||
pos = symbols[s] + 1;
|
||||
|
||||
pos = y.indexOf(s, ((pos > r) ? pos : r));
|
||||
if (pos === -1) { pos = n; }
|
||||
symbols[s] = pos;
|
||||
return pos;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
lcs_greedy('bcaaaade', 'deaaaabc'); // 'bc' instead of 'aaaa'
|
||||
Loading…
Add table
Add a link
Reference in a new issue