package 动态规划; /** * @ClassName 最长公共子串 * @Author ACER * @Description 例如asdb asd 最长公共子串是asd * @Date 2021/7/11 19:45 * @Version 1.0 **/ public class 最长公共子串 { static char[] chars2 = "ssdf".toCharArray(); static char[] chars1 = "asd".toCharArray(); public static void main(String[] args) { System.out.println(maxlen(chars1.length, chars2.length)); } private static int maxlen(int len1,int len2){ if (len1==0||len2==0){ return 0; } //将问题缩小 if (chars2[len2-1]==chars1[len1-1]){ return maxlen(len1-1,len2-1)+1; }else { return Math.max(maxlen(len1,len2-1),maxlen(len1-1,len2)); } } }