题目:求两个数组的最大公共子串
思路:典型的dp问题,状态转移是当两个相等就加1,压缩版和不压缩版写法都差不多~
public class Solution718 {
public static int findLength(int[] A, int[] B) {
int m = A.length;
int n = B.length;
int[][] dp = new int[m+1][n+1];
int res = 0;
for(int i = 0; i <= m; i ++){
for(int j = 0; j <= n; j ++){
if(i == 0 || j == 0){
dp[i][j] = 0;
continue;
}
if(A[i-1] == B[j-1]){
dp[i][j] = dp[i-1][j-1] + 1;
res = Math.max(res, dp[i][j]);
}
}
}
return res;
}
}