15.4.3
自顶向下的DP,按照标准的memoized递归写法即可。代码如下:
public static <T> void dp_topDown_findLCSLength(T[] x,T[] y,int[][] lcsLength){
for(int i=0;i<y.length;i++){
lcsLength[0][i] =0;
}
for(int i=0;i<x.length;i++){
lcsLength[i][0] =0;
}
// Initialize the auxiliary table
for(int i=1;i<x.length;i++){
for(int j=1;j<y.length;j++){
lcsLength[i][j] = -1;
}
}
// Call the memoized-type recursive function that actually performs the task.
memoized_findLCSLength(x, y, lcsLength, x.length-1, y.length-1);
}
private static <T> int memoized_findLCSLength(T[] x,T[] y,int[][] lcsLength,int i,int j){
if(lcsLength[i][j]>=0){
// Keep track of how many recursion calls are saved.
savedRecursions++;
return lcsLength[i][j];
}else{
int result = 0;
if(x[i] == y[j])
result = memoiz