C++--dp problem of two arrays (2)

1. Interleaved Strings  LeetCode official website - a technology growth platform loved by global geeks

Given three strings  s1, s2, s3, please judge  s3 whether they can be composed of  s1 and  interleaved (interleaved)  .s2 

 The definition and process of two strings sand t interleaving are as follows, where each string will be split into several non-empty substrings:

  • s = s1 + s2 + ... + sn
  • t = t1 + t2 + ... + tm
  • |n - m| <= 1
  • intertwined is s1 + t1 + s2 + t2 + s3 + t3 + ...ort1 + s1 + t2 + s2 + t3 + s3 + ...

Hint:a + b means string aand bconcatenation.

Example 1:

Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac"
 Output: true

Example 2:

Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbbaccc"
 Output: false

Example 3:

Input: s1 = "", s2 = "", s3 = ""
 Output: true
class Solution {
public:
    bool isInterleave(string s1, string s2, string s3) 
    {
        int n=s1.size();
        int m=s2.size();
       if(m+n!=s3.size())
        return false;
        s1=" "+s1;
        s2=" "+s2;
        s3=" "+s3;
        //初始化
        vector<vector<bool>> dp(n+1,vector<bool>(m+1));
        dp[0][0]=true;
        for(int j=1;j<=m;j++)
        {
            if(s2[j]==s3[j])
                dp[0][j]=true;
            else break;
        }
        for(int i=1;i<=n;i++)
        {
            if(s1[i]==s3[i])
                dp[i][0]=true;
            else break;
        }
        //状态转移方程
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                //两种方法都可
                /*if(s1[i]==s3[i+j]&&dp[i-1][j])
                {
                    dp[i][j]=true;
                }
                else if(s2[j]==s3[i+j]&&dp[i][j-1])
                {
                    dp[i][j]=true;
                }*/
                dp[i][j]=((dp[i-1][j]&&s1[i]==s3[i+j])||
                            (dp[i][j-1]&&s2[j]==s3[i+j]));
            }
        }
        return dp[n][m];
    }
};

2. Minimal ASCII deletion of two character strings and LeetCode official website - a technology growth platform loved by global geeks

Given two strings s1 sum  s2, return the minimum sum of the  ASCII  values ​​of the characters removed to make the two strings equal  .

Example 1:

Input: s1 = "sea", s2 = "eat"
 Output: 231
 Explanation: Remove "s" from "sea" and add the value of "s" (115) to the sum. 
Remove the "t" in "eat" and add 116 to the sum. 
At the end, the two strings are equal, and 115 + 116 = 231 is the minimum sum that meets the criteria.

Example 2:

Input: s1 = "delete", s2 = "leet"
 Output: 403
 Explanation: Delete "dee" in "delete" to change the string to "let", and convert 
100[d]+101[e]+101[e] Join the sum. Removing the "e" in "leet" adds 101[e] to the sum. 
At the end, both strings are equal to "let", the result is 100+101+101+101 = 403. 
If we instead converted the two strings to "lee" or "eet", we would get a result of 433 or 417, which is greater than the answer.

analyze:

class Solution {
public:
    int minimumDeleteSum(string s1, string s2) 
    {
        int n=s1.size(),m=s2.size();
        vector<vector<int>> dp(n+1,vector<int>(m+1));
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
                if(s1[i-1]==s2[j-1])
                {
                    dp[i][j]=max(dp[i][j],dp[i-1][j-1]+s1[i-1]);
                }
            }
        }
        int sum1=0;
        int sum2=0;

        for(auto sh1:s1) sum1+=sh1;
        for(auto sh2:s2) sum2+=sh2;
        return sum1+sum2-dp[n][m]*2;
    }
};

3. The longest repeated subarray  LeetCode official website - the technology growth platform loved by global geeks

Given two integer arrays  nums1 and  nums2 , return the length of the longest subarray  common to both arrays .

Example 1:

Input: nums1 = [1,2,3,2,1], nums2 = [3,2,1,4,7]
 Output: 3
 Explanation: The longest common subarray is [3,2,1].

Example 2:

Input: nums1 = [0,0,0,0,0], nums2 = [0,0,0,0,0]
 Output: 5
class Solution {
public:
    int findLength(vector<int>& nums1, vector<int>& nums2) 
    {
        int n=nums1.size();
        int m=nums2.size();
        vector<vector<int>> dp(n+1,vector<int>(m+1));
        int ret=0;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                if(nums1[i-1]==nums2[j-1]) 
                {
                    dp[i][j]=dp[i-1][j-1]+1;
                }
                else
                {
                    dp[i][j]=0;
                }
                ret=max(ret,dp[i][j]);
            }
            
        }
        return ret;
    }
};

4. Regular expression matching  LeetCode official website - a technology growth platform loved by global geeks

Given a string  s and a character rule  p, please implement a  regular expression matching that supports '.' and  .'*'

  • '.'matches any single character
  • '*'matches zero or more of the preceding element

The so-called match is to cover  the entire  string  s, not part of the string.

 

Example 1:

Input: s = "aa", p = "a"
 Output: false
 Explanation: "a" cannot match the entire string of "aa".

Example 2:

Input: s = "aa", p = "a*"
 Output: true
 Explanation: Because '*' means that it can match zero or more of the preceding element, and here the preceding element is 'a'. Therefore, the string "aa" can be considered as 'a' repeated once.

Example 3:

Input: s = "ab", p = ".*"
 Output: true
 Explanation: ".*" means that zero or more ('*') any character ('.') can be matched.
lass Solution {
public:
    bool isMatch(string s, string p) 
    {
        int n=s.size();
        int m=p.size();
        vector<vector<bool>> dp(n+1,vector<bool>(m+1));
        s=" "+s;
        p=" "+p;
        dp[0][0]=true;
        //初始化
        for(int i=2;i<=m;i+=2)
        {
            if(p[i]=='*')
                dp[0][i]=true;
            else break;
        }
        for(int i=1;i<=n;i++)
        {
            //状态转移方程
            for(int j=1;j<=m;j++)
            {
                if(s[i]==p[j]&&dp[i-1][j-1])
                    dp[i][j]=true;
                else if(p[j]=='.'&&dp[i-1][j-1])
                    dp[i][j]=true;
                else if(p[j]=='*')
                {
                    dp[i][j]=dp[i][j-2] || (p[j-1]=='.'||s[i]==p[j-1]) && dp[i-1][j];
                }

            }
        }
        return dp[n][m];//返回值
    }
};

5. Wildcard matching  LeetCode official website - the technology growth platform loved by global geeks

Given an input string ( s) and a character pattern ( p), please implement a wildcard matching that supports '?'and matching rules:'*'

  • '?'Can match any single character.
  • '*'Can match any character sequence (including the null character sequence).

The necessary and sufficient condition for judging a successful match is: the character pattern must be able to completely match the input string (not a partial match).

 

Example 1:

Input: s = "aa", p = "a"
 Output: false
 Explanation: "a" cannot match the entire string of "aa".

Example 2:

Input: s = "aa", p = "*"
 Output: true
 Explanation: '*' can match any string.

Example 3:

Input: s = "cb", p = "?a"
 Output: false
 Explanation: '?' can match 'c', but the second 'a' cannot match 'b'.
class Solution {
public:
    bool isMatch(string s, string p) 
    {
        int n=s.size();
        int m=p.size();
        vector<vector<bool>> dp(n+1,vector<bool>(m+1));
        dp[0][0]=true;
        for(int i=1;i<=m;i++)
        {
            if(p[i-1]=='*')
                dp[0][i]=true;
            else break;
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                if(p[j-1]=='*')
                {
                    dp[i][j]=dp[i-1][j]||dp[i][j-1];
                }
                else
                {
                   if(s[i-1]==p[j-1]&&dp[i-1][j-1])
                        dp[i][j]=true;
                    else if(p[j-1]=='?'&&dp[i-1][j-1])
                        dp[i][j]=true;
                }
            }
        }
        return dp[n][m];
    }
};

Guess you like

Origin blog.csdn.net/weixin_66828150/article/details/132524462