-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLCS.js
111 lines (94 loc) · 3.03 KB
/
LCS.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
function LCS(str1, str2) {
var sequence = [];
var result = '';
str1.split('').forEach(function(element) {
var index = str2.split('')
.map(function(target, idx) {
if(target === element){
return idx;
}
})
.filter(function(val){
return val >= 0;
});
if (index.length > 0) {
sequence.push(index.reverse().slice(0));
}
});
if (sequence.length === 0) {
return result;
}
var subSequence = sequence.length > 1 ? LIS(sequence.reduce((a, b) => a.concat(b))) : sequence;
console.log("subSequence = " + subSequence);
subSequence.forEach(function(index) {
result += str2[index];
});
return result;
}
var LIS = function(nums) {
console.log("target = " + nums);
var dp = [];
var subSequence = {
value: [],
highestPos: Number.MAX_VALUE
};
var sequence = {
value: [],
pos: []
};
var maxLength = getLength(nums);
console.log("maxLength = " + maxLength);
if(maxLength > 0){
for(var start = maxLength - 1; start >= 0; start--){
var index = sequence.pos.map(function(element, idx) {
if(element === start && idx < subSequence.highestPos){
return idx;
}
}).filter(function(val){
return val >= 0;
}).pop();
console.log("index = " + index);
if(index !== -1){
subSequence.value.unshift(sequence.value[index]);
subSequence.highestPos = index;
}
}
}
return subSequence.value;
function getLength(nums) {
var i, len;
for(i = 0; i < nums.length; i++){
len = dp.length;
sequence.value.push(nums[i]);
console.log("num = " + nums[i]);
console.log("len = " + len);
if(len === 0 || dp[len - 1] < nums[i]){
dp[len] = nums[i];
sequence.pos.push(len);
}else{
var findIdex = binarySearch(nums[i], 0, len - 1);
console.log("binarySearch = " + findIdex);
dp[findIdex] = nums[i];
sequence.pos.push(findIdex);
}
//console.log("dp = " + dp);
}
console.log("sequence.value = " + sequence.value);
console.log("sequence.pos = " + sequence.pos);
return dp.length;
}
function binarySearch(num, i, j){
if(i === j){
return i;
}
var middle = parseInt((i + j) / 2);
if(dp[middle] < num){
return binarySearch(num, middle + 1, j);
}
return binarySearch(num, i, middle);
}
};
// anothertest, notatest
//var result = LCS('abacd', 'dbaabca');
var result = LCS('anothertest', 'notatest');
console.log("result = " + result);