import java.util.Scanner; public class Main { public static void main(String[] args){ Scanner in = new Scanner(System.in); while(in.hasNext()){ String A = in.nextLine(); String B = in.nextLine(); if(A.length() <= B.length()){ // solution1(A, B); solution2(A, B); }else{ // solution1(B, A); solution2(B, A); } } } /** * 滑动窗口 * @param A * @param B */ private static void solution1(String A, String B){ int m = A.length(); int result = 0; boolean found = false; for(int i = m; i>0; i--){ for(int j=0; j+i<=m; j++){ if(B.contains(A.substring(j, j+i))){ result = i; found = true; break; } } if(found){ break; } } System.out.println(result); } /** * 动态规划 dp[i][j]表示在较短字符串A中以第i个字符结尾,较长字符串B中以第j个字符结尾时的当前局部公共子串长度 * @param A * @param B */ private static void solution2(String A, String B){ int m = A.length(); int n = B.length(); int[][] dp = new int[m+1][n+1]; int result = 0; for(int i=1; i<=m; i++){ for(int j=1; j<=n; j++){ if(A.charAt(i-1) == B.charAt(j-1)){ dp[i][j] = dp[i-1][j-1] + 1; if(result < dp[i][j]){ result = dp[i][j]; } } } } System.out.println(result); } }