LeetCode The longest common subsequence Java dynamic programming has a diagram

Title description

Given two strings text1 and text2, return the length of the longest common subsequence of these two strings.

A subsequence of a character string refers to a new character string: it is a new character string formed by deleting some characters (or no characters) from the original string without changing the relative order of the characters.

For example ,

"ace""abcde" 的子序列,但 "aec" 不是 
"abcde" 的子序列。两个字符串的「公共子序列」
是这两个字符串所共同拥有的子序列。

If the two strings have no common subsequence, 0 is returned.

Example 1:

输入:text1 = "abcde", text2 = "ace" 
输出:3  
解释:最长公共子序列是 "ace",它的长度为 3

Example 2:

输入:text1 = "abc", text2 = "abc"
输出:3
解释:最长公共子序列是 "abc",它的长度为 3

Example 3:

输入:text1 = "abc", text2 = "def"
输出:0
解释:两个字符串没有公共子序列,返回 0

prompt:

1 <= text1.length <= 1000
1 <= text2.length <= 1000
输入的字符串只含有小写英文字符。

Source: LeetCode (LeetCode)
Link: https://leetcode-cn.com/problems/longest-common-subsequence
Copyright is owned by LeetCode . For commercial reprints, please contact the official authorization. For non-commercial reprints, please indicate the source.

Problem-solving ideas:
How about DP? What do we set for the title. Since it is two strings, we will set up a two-dimensional array dp[i][j], which represents the first i characters of the first character and the second The longest subsequence that can be formed by the first j characters of the characters. The example shows that the subsequence can be discontinuous, so the longest affirmation dp[t1.length][t2.length].
Then we look at the last step,

1. What are if t1.charAt(t1.length-1)和t2.charAt(t2.length-1)不相等;
we going to do? Is directly equal to dp[t1.length-1-1][t2.length-1-1]? No, because this subsequence can be discontinuous, let's draw a picture:When the last length is not equal

Say yes 不包含两者中的至少一个, why not include dp[i-1][j-1]? It is possible to add it to the state transition equation, and the correct answer can be obtained, but it is not necessary, because dp[i-1][j-1] must be 小于等于dp[i][j-1], dp[i-1] [j], max is impossible to get it. The larger i and j, the more the same palindrome that it may contain. It will be clear if you draw it on paper. I am lazy to not draw it hahahaha

2. In the case of equality, directly dp[i][j]=dp[i-1][j-1]+1; this should be well understood
and then code:

class Solution {
    
    
    public int longestCommonSubsequence(String text1, String text2) {
    
    
            char s1[]=text1.toCharArray();
            char s2[]=text2.toCharArray();
            int x=s1.length;
            int y=s2.length;
            int dp[][]=new int[x+1][y+1];
            for(int i=1;i<=x;i++){
    
    
                for(int j=1;j<=y;j++){
    
    
                    if(s1[i-1]==s2[j-1])
                    dp[i][j]=dp[i-1][j-1]+1;
                    else{
    
    
                        dp[i][j]=Math.max(dp[i-1][j],dp[i][j-1]);
                    }
                }
            }
            return dp[x][y];
    }
}

Guess you like

Origin blog.csdn.net/qq_44844588/article/details/107658930
Recommended