LintCode刷题笔记-- LongestCommonSquence

题目描述:

Given two strings, find the longest common subsequence (LCS).

Your code should return the length of LCS.

解题思路:

这一题是非常经典的动态规划问题,在解题思路上可以按照经典的动态规划的解法,这是在系统学习动态规划之后第一个解决的LintCode上的问题:

1.子问题划分

给出两个字符串的A,B,两个字符串长度分别为lenA,lenB,求出两个字符串的LCS:

这划分为子问题:A.subString(0,0) 与B的LCS,在此基础上A.subString(0,1)与B的LCS,依次类推,可以得到A.subString(0,lenA-2), A.subString(0,lenA-1) 与B的LCS。 按照A的方法也同样可以对B在长度上进行分解。这样可以形成字符串A与B长度为lenA*lenB的矩阵,此矩阵为记录状态的“备忘录”。

2.初始状态的定义

对于LCS矩阵的初始状态,对于第一行与第一列相对应的意义为,A与B的第一个元素作为一个长度为1的字符串与对方是否存在公共字符,若存在,所在位置坐标已经后续坐标全部置为1.

3.问题与子问题解的关系

dp[i][j]:字符串的A的子串dp(0,i)与字符串B的子串dp(0,j)之间LCS的长度

dp[i][j]的取值:当A[i] == B[j],A(0,i)与B(0,j)之间的LCS会较比A(0,i-1)与B(0,j-1)多1,因为多了1位公共字符,LCS的长度自然会增加1。

        当A[i]!=B[j], A(0,i)与B(0,j)之间的LCS会选择先前公共子串更多的部分作为下一步求解

4.边界条件

当达到A,B两者的最大长度时结束

5.参考代码:

 1 public int longestCommonSubsequence(String A, String B) {
 2         int lenA = A.length();
 3         int lenB = B.length();
 4
 5         if(lenA == 0 || lenB == 0){
 6             return 0;
 7         }
 8
 9         int[][] dp = new int[lenA][lenB];
10
11         if(A.charAt(0)==B.charAt(0)){
12             dp[0][0] = 1;
13         }
14
15         for(int i = 1; i < lenA; i++){
16             if(B.charAt(0)==A.charAt(i)){
17                 dp[i][0] = 1;
18             }else{
19                 dp[i][0] = dp[i-1][0];
20             }
21         }
22         for(int j = 1; j < lenB; j++){
23             if(A.charAt(0)==B.charAt(j)){
24                 dp[0][j] = 1;
25             }else{
26                 dp[0][j] = dp[0][j-1];
27             }
28         }
29
30
31         for(int i = 1; i<lenA; i++){
32             for(int j = 1; j<lenB; j++){
33                 dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1]);
34                 if(A.charAt(i) == B.charAt(j)){
35                     dp[i][j] = dp[i-1][j-1]+1;
36                 }
37
38
39             }
40         }
41         return dp[lenA-1][lenB-1];
42     }
时间: 2024-10-05 14:21:39

LintCode刷题笔记-- LongestCommonSquence的相关文章

LintCode刷题笔记(九章ladder PartOne)--BugFree

九章ladder的前半部分刷题笔记,在这次二刷的时候补上~ @ 2017.05.21 141 - sqrtx 二分答案 ---  binarySearch二分法 --- class Solution: """ @param x: An integer @return: The sqrt of x """ def sqrt(self, x): # write your code here if not x: return 0 start, end

LintCode刷题笔记-- Maximal Square

题目描述: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and return its area. Example For example, given the following matrix: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 Return 4. 解题思路: 1.这一题明显使用动态规划来解题,开始想法使用动态

LintCode刷题笔记-- BackpackIV

动态规划 描述: Given an integer array nums with all positive numbers and no duplicates, find the number of possible combinations that add up to a positive integer target. Example Given nums = [1, 2, 4], target = 4 The possible combination ways are: [1, 1,

LintCode刷题笔记-- PaintHouse 1&amp;2

动态规划 题目描述: There are a row of n houses, each house can be painted with one of the k colors. The cost of painting each house with a certain color is different. You have to paint all the houses such that no two adjacent houses have the same color. The

LintCode刷题笔记-- Maximum Product Subarray

动态规划 描述: Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2,4], the contiguous subarray [2,3] has the largest product = 6. 解题思路: 前面几道题有点儿过分依赖答案了,后面还是需要自己主动

LintCode刷题笔记-- BackpackII

标记: 动态规划 问题描述: Given n items with size Ai, an integer m denotes the size of a backpack. How full you can fill this backpack? Example If we have 4 items with size [2, 3, 5, 7], the backpack size is 11, we can select [2, 3, 5], so that the max size we

LintCode刷题笔记-- Distinct Subsequences

题目描述: Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the rel

LintCode刷题笔记-- Edit distance

描述: Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.) You have the following 3 operations permitted on a word: Insert a character Delete a character Replace a

LintCode刷题笔记-- Count1 binary

位运算 描述: Count how many 1 in binary representation of a 32-bit integer. 解题思路: 统计一个int型的数的二进制表现形式中1的个数1.与check power of 2中的解题形式非常相似,同样利用num&(num-1) 的结果来检查num中二进制形式上1的个数,区别在于Check Power of 2 是来检查是否存在1,而这一题主要是检查有几个1.2. 此题可以利用 num = num&(num-1) 每次进行与运算