伪代码
1.
//given a rod (length = n) and price i("pi" ,i =1,...,n)
//r[i] : the maximum value of a rod (whose length = n) , initialize to be -INF
memoized-cut-rod(p,n,r)
for i=1 to n
r[i]=-INF
return memoized-cut-rod-aux(p,n,r)
2.
memoized-cut-rod-aux(p,n,r)
if r[n]>=0 //compare with 0
return r[n]
//still need to consider the case when n=0
if n==0
q=0
else q=-INF
//the left part is settled !!! Continue to see how to cut the right part
for i=1 to n
q=max( q,p[i]+memoized-cut-rod-aux(p,n-i,r) )
r[n]=q
return r[n]
2.
buttom-up-cut-rod(p,n)
//initialize r[0]=0 , so that the rest (1,..,n) can be cauculated
r=new array
r[0]=0
for j=1 to n
q=-INF
for i=1 to j
q=max(q,p[i]+r[j-i])
r[j]=q
return r
/**
* following give r[n] with a detailed solution
**/
3.
extended-buttom-up-cut-rod(p,n)
r=new array
s=new array
r[0]=0
for j=1 to n
q=-INF
for i=1 to j
if q<p[i]+r[j-i]
q=p[i]+r[j-i]
s[j]=i // length=j ,first cut's position
r[j]=q
return r,s
4.
print-solution(p,n)
(r,s)=extended-buttom-up-cut-rod(p,n)
while n>0
print s[n]
n=n-s[n]
/**
* following are for matrix chain
**/
5.
//p is an array containing matrix's row's number and column's number:Pi-1,Pi references to Ai's n*m (n:rows, m:columns)
matrix-chain-order(p)
n=p.length-1 //!!!relation
let m[1...n,1...n] , s[1...n-1,2...n] be new table //table
for i=1 to n
m[i,i]=0
for l=2 to n
for i=1 to n-l+1
j=i+l-1
m[i,j]=INF
for k=i to j-1
q=m[i,k]+m[k+1,j]+p(i-1)*pk*pj //pay attention Pi-1 = Ai's row number , Pi=Ai's column number
if q<m[i,j]
m[i,j]=q
s[i,j]=k
return m and s
6.//!
print-optimal-matrix-chain(s,i,j)
if i==j
print "A"
else
print "("
print-optimal-matrix-chain(s,i,s[i,j])
print-optimal-matrix-chain(s,s[i,j]+1,j)
print ")"
7.
memoized-matrix-chain(p)
n=p.length-1
m[1...n,1...n]=new table
s[1...n-1,2...n]=new table
for i=1 to n
m[i,i]=0
for i=1 to n-1
for j=i+1 to n
m[i,j]=INF
return lookup-chain(p,m,s,1,n)
8.
lookup-chain(p,m,s,i,j)
if m[i,j]<INF
return m[i,j]
for k=i to j-1
q=lookup-chain(p,m,s,i,k)+lookup-chain(p,m,s,k+1,j)+p(i-1)*pk*pj
if q<m[i,j]
m[i,j]=q
s[i,j]=k
return m[i,j] // remember to return!
/**
* following are for longest common subsequence
**/
9.
//c[i,j]:i->Xi's length , j->Yj's length
lcs-length(X,Y)
n=X.length
m=Y.length
c[1...n,1...m]=new table
b[1...n,1...m]=new table
for i=0 to n
c[i,0]=0
for j=1 to m //i=0->(i,0) is the same as j=0->(0,j)
c[0,j]=0
for i=1 to n
for j=1 to m
if xi==yj
c[i,j]=c[i-1,j-1]+1 //remember+1
b[i,j]="<-"(西北45)
else if c[i-1,j]<=c[i,j-1]
c[i,j]=c[i,j-1]
b[i,j]="<-"(西)
else
c[i,j]=c[i-1,j]
b[i,j]="<-"(北)
return b,c
10.
print-LCS(b,i,j)
//if i/j=0,return . Because b[0,0] is not initialize and has no meaning
if i==0 || j==0
return
if b[i,j]=="<-"(西北45)
print-LCS(b,i-1,j-1)
print xi
else if b[i,j]="<-"(西)
print-LCS(b,i,j-1)
else
print-LCS(b,i-1,j)
11.
//pk : k出现的频率 qd : d出现的频率
//w[i,j] = Σpk+Σqd (k=i...j d=i-1...j)
optimal-BST(p,q,n)
//i=i,...,n+1 ; j=0,...,n why?
w[1...n+1,0...n]=new table
e[1...n+1,0...n]=new table
for i=1 to n+1 //why to n+1 ->
e[i,i-1]=q(i-1) // why e[i,i-1] need initialize -> see below
w[i,i-1]=q(i-1)
for l=1 to n
for i=1 to n-l+1
j=i+l-1
w[i,j]=w[i,j-1]+pj+qj
e[i,j]=INF
for r=i to j
q=e[i,r-1]+e[r+1,j]+w[i.j] // e[i,r-1]!
if e[i,j]>q
e[i,j]=q
s[i,j]=r
return e,s
/**
* following are for Minimum-edit-distance
**/
12.Minimum-edit-distance(A,B)
d[0...A.length,0...B.length]=new table
for i=0 to A.length
for j=0 to B.length
d[i,j]=INF
for i=0 to A.length
d[i,0]=i
for j=1 to B.length
d[0,j]=j
return count-distance(A,B,A.length,B.length)
13.count-distance(A,B,i,j)
if d[i,j]<INF
return d[i,j]
if A[i]==B[j]
flag=0
else
flag=1
d[i,j]=min(count-distance(i-1,j)+1,count-distance(i,j-1)+1,count-distance(i-1,j-1)+flag)
return d[i,j]