Java (i use Eclipse) Use the below skeleton to implement lcs_lenght(). Make sure
ID: 3603676 • Letter: J
Question
Java (i use Eclipse)
Use the below skeleton to implement lcs_lenght(). Make sure to return the length of the longest common subsequence LCS, instead of c and b. Please make sure the code works.
public class LCS {
public static int lcs_length (String X, String Y) {
/*
* fill the code here
*/
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(LCS.lcs_length("ABCBDAB", "BDCABA"));
System.out.println(LCS.lcs_length("ACCGGTCGAGTGCGCGGAAGCCGGCCGAA",
"GTCGTTCGGAATGCCGTTGCTCTGTAAA"));
}
}
Explanation / Answer
here is the code hope you like it
// start from here
public class LCS {
public static void main(String args[]) {
System.out.println(LCS.lcs_length("ABCBDAB", "BDCABA"));
System.out.println(LCS.lcs_length("ACCGGTCGAGTGCGCGGAAGCCGGCCGAA",
"GTCGTTCGGAATGCCGTTGCTCTGTAAA"));
}
public static int lcs_length(String X, String Y){
int m = X.length();
int n = Y.length();
int len = 0;
int[][] temp = new int[m][n];
for(int i=0; i<m; i++){
for(int j=0; j<n; j++){
if(X.charAt(i) == Y.charAt(j)){
if(i==0 || j==0){
temp[i][j]=1;
}else{
temp[i][j] = temp[i-1][j-1]+1;
}
if(len < temp[i][j])
len = temp[i][j];
}
}
}
return len;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.