[Dynamic Programming Algorithm Exercise] day11


1. 1312. Minimum number of insertions for a string to become a palindrome

1. Introduction to the topic

1312. The minimum number of insertions to make a string a palindrome. Given
a string s, you can insert any character at any position in the string with each operation.
Please return the minimum number of operations for s to become a palindrome string.
A "palindrome string" is a string that is the same in both forward and backward readings.
Insert image description here
Insert image description here

2. Problem-solving ideas

3.Code

class Solution {
    
    
public:
    int minInsertions(string s) {
    
    
        int n = s.size();
        //1.判断出字符串中最长回文子序列
        vector<int> v(n, 0);
        vector<vector<int>> dp(n, v);
        //dp[i][j]表示以i为结尾以j为开始的最长回文子序列元素个数
        //初始化
        for(int i = 0;i < n; ++i)
        {
    
    
            dp[i][i] = 1;
        }
        for(int i = 0;i < n; ++i)
        {
    
    
            for(int j = i - 1;j >= 0; --j)
            {
    
    
                if(s[i] == s[j])//如果相等,则回文子序列加2元素
                {
    
    
                    dp[i][j] = dp[i - 1][j + 1] + 2;
                }
                else//如果不相等,则它的回文子序列为i或j往前遍历一个位置对应的回文子序列的较大值
                {
    
    
                    dp[i][j] = max(dp[i - 1][j], dp[i][j + 1]);
                }
            }
        }
        int a = dp[n - 1][0];
        //2.用总元素个数减去最长回文子序列的元素个数
        return n - a;
    }
};

4. Running results

Insert image description here

2. 1143. Longest common subsequence

1. Introduction to the topic

1143. Longest common subsequence
Given two strings text1 and text2, return the length of the longest common subsequence of these two strings. If there is no common subsequence, return 0.
A subsequence of a string refers to a new string that is composed of the original string by deleting some characters (or not deleting any characters) without changing the relative order of the characters.
For example, "ace" is a subsequence of "abcde", but "aec" is not a subsequence of "abcde".
The common subsequence of two strings is the subsequence that both strings have in common.
Insert image description here

2. Problem-solving ideas

3.Code

class Solution {
    
    
public:
    int longestCommonSubsequence(string text1, string text2) {
    
    
        vector<int> v(text2.size() + 1, 0);
        vector<vector<int>> dp(text1.size() + 1, v);
        //dp[i][j]表示以i - 1为结尾的text1字符串和以j - 1为结尾的text2这两个字符串的最长公共子序列(为啥要-1呢?为了方便计算i - 1和j - 1的值,而不用很麻烦的初始化i=0和j=0的情况,实际上就是将i和j作为了长度而不是坐标)
        for(int i = 1;i <= text1.size(); ++i)
        {
    
    
            for(int j = 1;j <= text2.size(); ++j)
            {
    
    
                if(text1[i - 1] == text2[j - 1])//如果i和j对应元素相等,就等于它们前一个元素对应的最长长度加一
                {
    
    
                    dp[i][j] = dp[i - 1][j - 1] + 1;
                }
                else//如果不相等就等于它们分别前一个元素对应的最长长度的较大值
                {
    
    
                    dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
                }
            }
        }
        return dp[text1.size()][text2.size()];
    }
};

4. Running results

Insert image description here

3. 1035. Disjoint lines

1. Introduction to the topic

1035. Disjoint Lines
Write the integers in nums1 and nums2 in the given order on two independent horizontal lines.
Now, you can draw some straight lines connecting the two numbers nums1[i] and nums2[j]. These straight lines need to satisfy at the same time: nums1[i] == nums2[j]
and the drawn straight lines do not connect with any other lines (not horizontal lines) intersect.
Note that lines cannot intersect even at their endpoints: each number can belong to only one line.
Draws lines this way and returns the maximum number of connections that can be drawn.
Insert image description here
Insert image description here

2. Problem-solving ideas

3.Code

class Solution {
    
    
public:
    int maxUncrossedLines(vector<int>& nums1, vector<int>& nums2) {
    
    
        vector<int> v(nums2.size() + 1, 0);
        vector<vector<int>> dp(nums1.size() + 1, v);
        //实际上就是在求:以i为结尾的nums1数组和以j结尾的nums2数组两者之间的最长公共子序列
        for(int i = 1;i <= nums1.size(); ++i)
        {
    
    
            for(int j = 1;j <= nums2.size(); ++j)
            {
    
    
                if(nums1[i - 1] == nums2[j - 1])
                {
    
    
                    dp[i][j] = dp[i - 1][j - 1] + 1;
                }
                else
                {
    
    
                    dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
                }
            }
        }
        return dp[nums1.size()][nums2.size()];
    }
};

4. Running results

Insert image description here


Summarize

Today is the 11th day of algorithm practice.
There will be times when the wind and waves break, so hang your sails and sail across the sea , and keep on working hard.
Source of the title in this article: LeetCode, the copyright belongs to LeetCode Network.
If this article has inspired you, I hope you can support the author more, thank you all!

Guess you like

Origin blog.csdn.net/xjjxjy_2021/article/details/131455479