牛客 BM66 最长公共子串 【动态规划】

描述

给定两个字符串str1和str2,输出两个字符串的最长公共子串

题目保证str1和str2的最长公共子串存在且唯一。 

数据范围: 1str1,str25000

要求: 空间复杂度O(n2),时间复杂度O(n2)

示例1

输入:

"1AB2345CD","12345EF"

返回值:

"2345"

 dp背包问题

s1 = "1AB2345CD"

s2 = "12345EF"

当 s1 的 xPos 位置 = s2 的 yPos 的字符时,它的最大长度等于 dp[yPos-1][xPos-1] + 1,那么 dp 公式就出来了

    public String LCS (String str1, String str2) {
        // write code here
        if (str1 == null || str2 == null || str1.isEmpty() || str2.isEmpty()) {
            return "";
        }
        int length1 = str1.length();
        int length2 = str2.length();
        int[][] dpMap = new int[length2][length1];
        int xPos;
        int yPos;
        int maxXPos = 0;
        int maxLength = 0;
        // 初始化 dpMap
        for (xPos = 0; xPos < length1; xPos++) {
            char c = str2.charAt(0);
            if (c == str1.charAt(xPos)) {
                dpMap[0][xPos] = 1;
            }
        }
        for (yPos = 1; yPos < length2; yPos++) {
            char c = str1.charAt(0);
            if (c == str2.charAt(yPos)) {
                dpMap[yPos][0] = 1;
            }
        }
        for (yPos = 1; yPos < length2; yPos++) {
            for (xPos = 1; xPos < length1; xPos++) {
                if (str1.charAt(xPos) == str2.charAt(yPos)) {
                    int temp = dpMap[yPos-1][xPos-1] + 1;
                    dpMap[yPos][xPos] = temp;
                    if (temp > maxLength) {
                        maxXPos = xPos;
                        maxLength = temp;
                    }
                }
            }
        }
        return str1.substring(maxXPos-maxLength+1, maxXPos+1);
    }

 二维背包时间复杂度 O(n^2),空间复杂度 O(n^2)。但因为本体的特殊性,子串是连续的字符串,所以可以用反向的一维数组代替。

即 dp[xPos] = dp[xPos-1] + 1

此处 dp[xPos] 是这个循环的当前位置,而 dp[xPos-1] 是上个循环的当前位置

        for (yPos = 1; yPos < length2; yPos++) {
            for (xPos = length1-1; xPos >= 0; xPos--) {
                if (str1.charAt(xPos) == str2.charAt(yPos)) {
                    int temp = dpMap[xPos] + 1;
                    dpMap[xPos+1] = temp;
                    if (temp > maxLength) {
                        maxXPos = xPos;
                        maxLength = temp;
                    }
                } else {
                    dpMap[xPos+1] = 0;
                }
            }
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
动态规划在解决牛客删除括号问题时,可以按照以下步骤进行: 1. 首先,我们需要理解题目的需求。题目要求我们删除括号,使得剩下的字符满足以下条件:左括号和右括号的数量相等,且左括号的位置必须在右括号的前面。 2. 接下来,我们可以使用动态规划来解决这个问题。我们可以定义一个三维的dp数组,其中dp[q][w][e]表示考虑s前q个字符,t前w个字符,且s被删除部分左括号数减去右括号数为e时,是否可行(bool类型)。 3. 然后,我们可以从前向后遍历字符s和t。在每一步中,我们可以考虑两种情况: a. 删除的左括号数目比右括号多:我们可以继续删除左括号,或者删除右括号。即dp[q][w][e] = dp[q-1][w][e+1]或dp[q-1][w][e-1]。 b. 删除的左括号数目与右括号数目相同:我们只能删除右括号。即dp[q][w][e] = dp[q-1][w-1][e-1]。 4. 最后,我们可以根据dp[len1][len2][0]的值来判断是否可行。其中len1和len2分别表示字符s和t的长度。 综上所述,通过动态规划的思路,我们可以解决牛客删除括号的问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [动态规划笔记](https://download.csdn.net/download/weixin_38617297/13751806)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [牛客_21303删括号_动态规划](https://blog.csdn.net/weixin_45619006/article/details/114650172)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值