题解 | 公共子串计算
公共子串计算
https://www.nowcoder.com/practice/98dc82c094e043ccb7e0570e5342dd1b
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String x = in.nextLine(); String y = in.nextLine(); int max = 0; int length = 1; for (int i = 0; i < x.length(); i++) { for (int j = 0; j < x.length(); j++) { for (int k = j; k < x.length(); k++) { if(j + length <= x.length()) { String str = x.substring(j, j + length); if(y.contains(str)) { max = length; } } } } length++; } System.out.println(max); } }