国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Lintcode: Longest Common Subsequence

2019-11-14 23:10:45
字體:
來源:轉載
供稿:網友
Lintcode: Longest Common Subsequence
Given two strings, find the longest comment subsequence (LCS).Your code should return the length of LCS.ExampleFor "ABCD" and "EDCA", the LCS is "A" (or D or C), return 1For "ABCD" and "EACB", the LCS is "AC", return 2

題目里說了:

ClarificationWhat's the definition of Longest Common Subsequence?

*(Note that a subsequence is different from a substring, for the terms of the former need not be consecutive terms of the original sequence.) It is a classic computer science PRoblem, the basis of file comparison programs such as diff, and has applications in bioinformatics.

1. D[i][j] 定義為s1, s2的前i,j個字符串的最長common subsequence.

2. D[i][j]當char i == char j, 可以有三種選擇,D[i - 1][j - 1] + 1,D[i ][j - 1], D[i - 1][j] ,取最大的

當char i != char j,D[i ][j - 1], D[i - 1][j] 里取一個大的(因為最后一個不相同,所以有可能s1的最后一個字符會出現在s2的前部分里,反之亦然。

 1 public class Solution { 2     /** 3      * @param A, B: Two strings. 4      * @return: The length of longest common subsequence of A and B. 5      */ 6     public int longestCommonSubsequence(String A, String B) { 7         // write your code here 8         int[][] res = new int[A.length()+1][B.length()+1]; 9         for (int i=1; i<=A.length(); i++) {10             for (int j=1; j<=B.length(); j++) {11                 res[i][j] = Math.max(A.charAt(i-1)==B.charAt(j-1)? res[i-1][j-1]+1 : res[i-1][j-1], 12                 Math.max(res[i-1][j], res[i][j-1]));13             }14         }15         return res[A.length()][B.length()];16     }17 }


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 交城县| 东明县| 南京市| 平昌县| 原阳县| 化州市| 江孜县| 石林| 西和县| 河北省| 漾濞| 宝兴县| 郯城县| 兴化市| 台中市| 罗甸县| 金乡县| 梅河口市| 观塘区| 平塘县| 洪洞县| 南江县| 大兴区| 宁乡县| 布尔津县| 新干县| 江达县| 忻州市| 砀山县| 武陟县| 龙山县| 雷山县| 甘洛县| 汝城县| 普安县| 武鸣县| 潮安县| 五台县| 孝昌县| 平湖市| 绥阳县|