题解 | #公共子串计算#
公共子串计算
https://www.nowcoder.com/practice/98dc82c094e043ccb7e0570e5342dd1b
非常经典的动态规划问题了
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String a = in.nextLine(), b = in.nextLine(); int max = 0; int[][] dp = new int[a.length() + 1][b.length() + 1]; for (int i = 1; i < dp.length; i++) { for (int j = 1; j < dp[i].length; j++) { if (a.charAt(i - 1) == b.charAt(j - 1)) { dp[i][j] = dp[i - 1][j - 1] + 1; } max = Math.max(max, dp[i][j]); } } System.out.println(max); } }