Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
|
|
@ -0,0 +1,14 @@
|
|||
function lcs(a, b) {
|
||||
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);
|
||||
} else {
|
||||
var x = lcs(a, bSub);
|
||||
var y = lcs(aSub, b);
|
||||
return (x.length > y.length) ? x : y;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
const longest = (xs, ys) => (xs.length > ys.length) ? xs : ys;
|
||||
|
||||
const lcs = (xx, yy) => {
|
||||
if (!xx.length || !yy.length) { return ''; }
|
||||
|
||||
const [x, ...xs] = xx;
|
||||
const [y, ...ys] = yy;
|
||||
|
||||
return (x === y) ? (x + lcs(xs, ys)) : longest(lcs(xx, ys), lcs(xs, yy));
|
||||
};
|
||||
|
|
@ -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('');
|
||||
}
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
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;
|
||||
}
|
||||
}
|
||||
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