Lintcode - Longest common subsequence
2015-02-04 13:05
726 查看
Given two strings, find the longest comment subsequence (LCS).
Your code should return the length of LCS.
样例
For "ABCD" and "EDCA", the LCS is "A" (or D or C), return 1
For "ABCD" and "EACB", the LCS is "AC", return 2
2d: 一维矩阵应该做不了,因为d[i+1][j+1] 与 d[i][j+1] d[i+1][j] d[i][j]都有关
Your code should return the length of LCS.
样例
For "ABCD" and "EDCA", the LCS is "A" (or D or C), return 1
For "ABCD" and "EACB", the LCS is "AC", return 2
2d: 一维矩阵应该做不了,因为d[i+1][j+1] 与 d[i][j+1] d[i+1][j] d[i][j]都有关
public int longestCommonSubsequence(String A, String B) { int[][] d = new int[A.length()+1][B.length()+1]; for (int i = 0; i < A.length(); i++) { for (int j = 0; j < B.length(); j++) { if (A.charAt(i) == B.charAt(j)) { d[i+1][j+1] = d[i][j]+1; } else { d[i+1][j+1] = Math.max(d[i][j+1], d[i+1][j]); } } } return d[A.length()][B.length()]; }
相关文章推荐
- Lintcode: Longest Common Subsequence
- lintcode longest-common-subsequence 最长公共子序列 证明
- LintCode Longest Common Subsequence
- LintCode Longest Common Subsequence(最长公共子序列长度,动态规划入门题)
- LintCode Longest Common Substring
- lintcode :longest common substring 最长公共子串
- lintcode-medium-Longest Common Prefix
- lintcode: Longest Common Prefix
- LintCode Longest Increasing Continuous Subsequence
- lintcode:Longest Increasing Continuous subsequence II
- LintCode: Longest Increasing Continuous subsequence
- LintCode Longest Common Substring
- lintcode longest-increasing-continuous-subsequence 最长上升连续子序列
- LintCode-Longest Common Substring
- LintCode Longest Increasing Continuous subsequence II
- codewars—Longest Common Subsequence
- lintcode-easy-Longest Increasing Continuous Subsequence
- LintCode "Longest Increasing Subsequence"
- lintcode:Longest Common Substring
- LintCode "Longest Increasing Continuous subsequence II" !!