题解 | #公共子串计算#
公共子串计算
https://www.nowcoder.com/practice/98dc82c094e043ccb7e0570e5342dd1b
import java.util.Scanner; public class Main { public static void main(String[] args) { int max =0; Scanner scanner = new Scanner(System.in); String str1 = scanner.nextLine(); String str2 = scanner.nextLine(); int [][] num = new int[str1.length()+1][str2.length()+1]; for(int i = 1;i <= str1.length();i++){ for(int j =1;j<=str2.length();j++){ if(str1.charAt(i-1)==str2.charAt(j-1)){ num[i][j] = num[i-1][j-1] +1; if(num[i][j]>=max) max = num[i][j]; }else num[i][j] = 0; } } System.out.println(max); } }