package com.cjh.dp;
import com.sun.swing.internal.plaf.basic.resources.basic;
public class Dp2 {
public static void main(String[] args) {
//求两个字符串的最长公共子串
method("itheima","thema");
}
private static void method(String a, String b) {
int max = 0;
// TODO Auto-generated method stub
int[][] dp = new int[b.length()][a.length()];
for (int i = 0; i < b.length(); i++) {
for (int j = 0; j < a.length(); j++) {
if (a.charAt(j) == b.charAt(i)) {
if(i == 0 || j == 0) {
dp[i][j] = 1;
}else {
dp[i][j] = dp[i - 1][j - 1] + 1;
}
max = Math.max(max, dp[i][j]);
}else {
dp[i][j] = 0;
}
}
}
System.out.println(max);
}
}
求两个字符串的最长公共字串(dp)
最新推荐文章于 2026-01-08 20:20:31 发布

2432

被折叠的 条评论
为什么被折叠?



