题解 | #公共子串计算#

公共子串计算

https://www.nowcoder.com/practice/98dc82c094e043ccb7e0570e5342dd1b

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner fzhinput = new Scanner(System.in);
        String zfc1 = fzhinput.nextLine();
        String zfc2 = fzhinput.nextLine();
        if (zfc1.length() < zfc2.length()) {
            System.out.println(maxlengthes(zfc1, zfc2));
        } else {
            System.out.println(maxlengthes(zfc2, zfc1));
        }
    }
    private static int maxlengthes(String zfc1, String zfc2) {
        int zfcl1 = zfc1.length();
        int zfcl2 = zfc2.length();
        int maxlength = 0;
        int dp[][] = new int[zfcl1 + 1][zfcl2 + 1];
        for (int i = 1; i <= zfcl1; i++) {
            for (int j = 1; j <= zfcl2; j++) {
                if (zfc1.charAt(i - 1) == zfc2.charAt(j - 1)) {
                    dp[i][j] = dp[i - 1][j - 1] + 1;
                    maxlength = Math.max(maxlength, dp[i][j]);

                }
            }
        }
        return maxlength;
    }
}

全部评论

相关推荐

点赞 评论 收藏
分享
10-07 20:48
门头沟学院 Java
不敢追175女神:可能是实习上着班想到后面还要回学校给导师做牛马,看着身边都是21-25的年纪,突然emo了了
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务