練習題
限制 :
1 <= text1.length, text2.length <= 1000
text1
andtext2
consist of only lowercase English characters.
利用 Dynamic programming 計算最長子序列。
演算法:
建立一個從 text1 –> text2 的表格,如下圖所示:
LCS | "" | text2[0] | text2[1] | … | … | text2[0] |
---|---|---|---|---|---|---|
"" | ||||||
text1[0] | ||||||
text1[1] | ||||||
… | ||||||
… | ||||||
text1[text1.size()-1] |
比較 text1[m] 和 text2[n] 是否相同
max(i > 0 ? record[i-1][j] : 0, j > 0 ? record[i][j-1] : 0)
reference : link