·  阅读 235

# 高级算法

## 斐波那契数列

// 0, 1, 1 ,2 ,3 ,5, 8, 13....

``````function Fib(n) {
var i = 0
if (n < 2) {
return n
} else {
return Fib(n-1) + Fib(n-2)
}
}

`````` function iterFib(n) {
var last = 1
var nextLast = 1
var result = 1
for (var i = 2; i < n; n++) {
result = last + nextLast
nextLast = last
last = result
}
return result
}

## 寻找最长公共子串

reaven havoc 公共的最长子串是av

``````function lcs(word1, word2) {
var max = 0
var index = 0
var lcsarr = new Array(word1.length + 1)
for (var i = 0; i <= word1.length + 1; i++) {
lcsarr[i] = new Array(word2.length + 1)
for (var j = 0; j <= word2.length + 1; ++j) {
lcsarr[i][j] = 0
}
}
for (var i = 0; i <= word1.length + 1; i++) {
for (var j = 0; j <= word2.length + 1; ++j) {
if (i == 0 || j == 0) {
lcsarr[i][j] = 0
} else {
if (word1[i - 1] == word2[i - 1]) {
lcsarr[i][j] = lcsarr[i - 1][j - 1] + 1
} else {
lcsarr[i][j] = 0
}
}
if (max < lcsarr[i][j]) {
max = lcsarr[i][j]
index = i
}
}
}
var str = ''
if (max == 0) {
return ''
} else {
for (var i = index - max; i <= max; i++) {
str += word2[i]
}
return str
}
}

## 背包问题

``````    // 背包问题
function max(a, b) {
return (a > b) ? a : b
}

function knapsack(capacity, size, value, n) {
if (n == 0 || capacity == 0) {
return 0
}
if (size[n - 1] > capacity) {
return knapsack(capacity, size, value, n - 1)
} else {
return max(value[n - 1] +
knapsack(capacity - size[n - 1], size, value, n - 1),
knapsack(capacity, size, value, n - 1))
}
}

``````function DKnapsack(capacity, size, value, n) {
var K = []
for (var i = 0; i <= capacity; i++) {
K[i] = []
}
for (var i = 0; i <= n; i++) {
for (var w = 0; w <= capacity; w++) {
if (i == 0 || w == 0) {
K[i][w] = 0
} else if (size[i-1] <= w) {
K[i][w] = max(value[i-1] + K[i-1][w-size[i-1]], K[i-1][w])
} else {
K[i][w] = K[i-1][w]
}
console.log(K[i][w] + ' ')
}
}
return K[n][capacity]
}