其他分享
首页 > 其他分享> > 牛客华为机试HJ75

牛客华为机试HJ75

作者:互联网

原题传送门

1. 问题描述

2. Solution

1、思路: DP
首先,引入问题最长公共子串(Longest Common Substring),原题见 NC127
1) 状态定义
dp[i][j] 表示字符串s1中第i个字符和s2中第j个字符所构成的最长公共子串。
2) 初始状态
dp[i][j] = {0}
3) 状态转移方程
dp[i][j] = 0, if s1[i] != s2[j]
dp[i][j] = dp[i-1][j-1] + 1, if s1[i] == s2[j]
状态转移方程中出现了 i - 1,要求必须 i>=1,遍历字符串的时候,i从0开始。
故,对上面的公式做个替换 i -> i + 1,导出
dp[i+1][j+1] = dp[i][j] + 1, if s1[i] == s2[j]

2、示例

String s1 = "1AB2345CD", s2 = "12345EF", expected = "2345";

手动模拟结果:
image.png
3、代码实现

/*
    DP
    1) 状态定义
        dp[i][j] 表示字符串s1中第i个字符和s2中第j个字符所构成的最长公共子串。
    2) 初始状态
        dp[i][j] = {0}
    3) 状态转移方程
        dp[i][j] = 0, if s1[i] != s2[j]
        dp[i][j] = dp[i-1][j-1] + 1, if s1[i] == s2[j]
 */
public class Solution {

    @Test
    public void test1() {
        String s1 = "1AB2345CD", s2 = "12345EF", expected = "2345";
        assertEquals(expected, LCS(s1, s2));
    }

    /**
     * longest common substring
     *
     * @param s1 string字符串 the string
     * @param s2 string字符串 the string
     * @return string字符串
     */
    public String LCS(String s1, String s2) {
        // write code here
        int maxLen = 0;
        int end = 0;
        int m = s1.length(), n = s2.length();
        int[][] dp = new int[m + 1][n + 1];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (s1.charAt(i) == s2.charAt(j)) {
                    dp[i + 1][j + 1] = dp[i][j] + 1;
                    if (dp[i + 1][j + 1] > maxLen) {
                        maxLen = dp[i + 1][j + 1];
                        end = i;
                    }
                }
            }
        }
        return s1.substring(end - maxLen + 1, end + 1);
    }
}
time complexity: 两层循环,O(n^2)
space complexity: 二维数组,O(n^2)

4、优化,DP使用一维数组

/*
    DP使用一维数组
 */
public class Solution1 {
    public String LCS(String s1, String s2) {
        int maxLen = 0;
        int end = 0;
        int m = s1.length(), n = s2.length();
        int[] dp = new int[n + 1];
        for (int i = 0; i < m; i++) {
            for (int j = n - 1; j >= 0; j--) {
                if (s1.charAt(i) == s2.charAt(j)) {
                    dp[j + 1] = dp[j] + 1;
                    if (dp[j + 1] > maxLen) {
                        maxLen = dp[j + 1];
                        end = i;
                    }
                } else dp[j + 1] = 0;
            }
        }
        return s1.substring(end - maxLen + 1, end + 1);
    }
}
time complexity: 两层循环,O(n^2)
space complexity: 一维数组,O(n )

5、本题解答

import sys

if sys.platform != "linux":
    file_in = open("input/HJ75.txt")
    sys.stdin = file_in

"""
    DP
    1) 状态定义
        dp[i][j] 表示字符串s1中第i个字符和s2中第j个字符所构成的最长公共子串。
    2) 初始状态
        dp[i][j] = {0}
    3) 状态转移方程
        dp[i][j] = 0, if s1[i] != s2[j]
        dp[i][j] = dp[i-1][j-1] + 1, if s1[i] == s2[j]
"""

def solve(s1, s2):
    max_len = 0
    m = len(s1)
    n = len(s2)
    dp = [[0] * (n + 1) for _ in range(m + 1)]
    for i in range(m):
        for j in range(n):
            if s1[i] == s2[j]:
                dp[i + 1][j + 1] = dp[i][j] + 1
                if dp[i + 1][j + 1] > max_len:
                    max_len = dp[i + 1][j + 1]
    print(max_len)


while True:
    try:
        s1 = input().strip()
        s2 = input().strip()
        solve(s1, s2)
    except:
        break

标签:String,maxLen,int,s2,s1,牛客,HJ75,机试,dp
来源: https://www.cnblogs.com/junstat/p/16177287.html