動態(tài)規(guī)劃的方程大家都知道,就是
f[i,j]=min{f[i-1,j-1],f[i-1,j],f[i,j-1],f[i,j+1]}+a[i,j]
但是很多人會懷疑這道題的后效性而放棄動規(guī)做法。
本來我還想做Dijkstra,后來變了沒二十行pascal就告訴我數(shù)組越界了……(dist:array[1..1000*1001
div 2]...)
無奈之余看了xj_kidb1的題解,剛開始還覺得有問題,后來豁然開朗……
反復(fù)動規(guī)。上山容易下山難,我們可以從上往下走,最后輸出f[n][1]。
xj_kidb1的一個技巧很重要,每次令f[i][0]=f[i][i],f[i][i+1]=f[i][1](xj_kidb1的題解還寫錯了)
標簽:
動態(tài)規(guī)劃
方程
家
上傳時間:
2014-07-16
上傳用戶:libinxny
Instead of finding the longest common
subsequence, let us try to determine the
length of the LCS.
Then tracking back to find the LCS.
Consider a1a2…am and b1b2…bn.
Case 1: am=bn. The LCS must contain am,
we have to find the LCS of a1a2…am-1 and
b1b2…bn-1.
Case 2: am≠bn. Wehave to find the LCS of
a1a2…am-1 and b1b2…bn, and a1a2…am and
b b b
b1b2…bn-1
Let A = a1 a2 … am and B = b1 b2 … bn
Let Li j denote the length of the longest i,g g
common subsequence of a1 a2 … ai and b1 b2
… bj.
Li,j = Li-1,j-1 + 1 if ai=bj
max{ L L } a≠b i-1,j, i,j-1 if ai≠j
L0,0 = L0,j = Li,0 = 0 for 1≤i≤m, 1≤j≤n.
標簽:
the
subsequence
determine
Instead
上傳時間:
2013-12-17
上傳用戶:evil