public class DebugTest {
public static void main(String[] args) {
char X[] = "AGGTAB".toCharArray();
char Y[] = "GXTXAYB".toCharArray();
System.out.println(findLCS_dp1(X, Y));
X = "XMJYAUZ".toCharArray();
Y = "MZJAWXU".toCharArray();
System.out.println(findLCS_dp1(X, Y));
}
public static int findLCS_dp1(char[] a, char[] b){
int m = b.length;
int n= a.length;
int[] lcs = new int[n+1];
for(int i=1;i<=n;i++){
int prevMatchedValueAtIndexJ = lcs[0];
for(int j=1; j<=m; j++){
int temp = lcs[j] ;
if(a[i-1] == b[j-1])
lcs[j] = 1 + prevMatchedValueAtIndexJ;
else
lcs[j] = Math.max(lcs[j-1], lcs[j]);
prevMatchedValueAtIndexJ = temp;
}
}
return lcs[m];
}
}
No comments:
Post a Comment