return return 1return 2
The title says:
Clarification
What ' s the definition of longest Common subsequence?
* (Note that a subsequence are different from a substring, for the terms of the former need isn't be consecutive terms of the Original sequence.) It is a classic computer problem, the basis of file comparison programs such as diff, and have applications in Bioi Nformatics.
1. d[i][j] is defined as S1, s2 the longest common subsequence of the first i,j string.
2. d[i][j] when char i = = Char J, there can be three options, d[i-1][j-1] + 1,d[i][j-1], D[i-1][j], take the largest
When char i! = char J, d[i][j-1], D[i-1][j] takes a large (because the last one is not the same, it is possible that the last character of the S1 appears in the first part of the S2, and vice versa.
1 Public classSolution {2 /**3 * @paramA, b:two strings.4 * @return: The length of longest common subsequence of A and B.5 */6 Public intlongestcommonsubsequence (String A, String B) {7 //Write your code here8 int[] res =New int[A.length () +1] [B.length () +1];9 for(intI=1; I<=a.length (); i++) {Ten for(intJ=1; J<=b.length (); J + +) { OneRES[I][J] = Math.max (A.charat (i-1) ==b.charat (j-1)? res[i-1][j-1]+1:res[i-1][j-1], AMath.max (Res[i-1][j], res[i][j-1])); - } - } the returnres[a.length ()][b.length ()]; - } -}
Lintcode:longest Common subsequence