import java.util.*; import java.io.*; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String a = br.readLine(); String b = br.readLine(); //使a为小串,减少循环次数 if (a.length() > b.length()) { String temp = a; a = b; b = temp; } //1.此处使用标准的动态规划会使用n^2的时间和空间复杂度,不符合题目的进阶要求,需要使用n^3时间n空间复杂度 //2.原:dp[i][j]:a[i]及b[j]为尾的最长公共子串->dp[i][j]= a[i]==b[j]?dp[i-1][j-1]+1:0 //3.现:暴力求解,将a从大到小截取字符串与b对比,知道b包含截取的字符串为止 int max = 0; //先循环需要截取的长度 for (int n = a.length(); n > 0; n--) { //循环截取的起始下标 for (int j = 0; j < a.length() - n + 1; j++) { if (b.contains(a.substring(j, j + n))) { System.out.println(n); return; } } } System.out.println(0); } }