台中女中程式解題系統:b033: 兩隻猴子
1 min readNov 16, 2019
題目連結 http://www.tcgs.tc.edu.tw:1218/ShowProblem?problemid=b033。
此題用 Dynamic Programming ,可參考 https://en.wikipedia.org/wiki/Longest_common_subsequence_problem#Computing_the_length_of_the_LCS 。
演算法(底下文字是從https://en.wikipedia.org/wiki/Longest_common_subsequence_problem#Computing_the_length_of_the_LCS 節錄出來)
function LCSLength(X[1..m], Y[1..n])
C = array(0..m, 0..n)
for i := 0..m
C[i,0] = 0
for j := 0..n
C[0,j] = 0
for i := 1..m
for j := 1..n
if X[i] = Y[j]
C[i,j] := C[i-1,j-1] + 1
else
C[i,j] := max(C[i,j-1], C[i-1,j])
return C[m,n]
程式碼….