题解 | #公共子串计算#
公共子串计算
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); while (in.hasNext()) { String str1 = in.nextLine().toLowerCase(); String str2 = in.nextLine().toLowerCase(); if (str1.length() > str2.length()) { String gg = str1; str1 = str2; str2 = gg; } int sum = 0; for (int i = 0 ; i < str1.length(); i++) { for (int j = str1.length(); j > i; j--) { String gg = str1.substring(i, j); if (str2.contains(gg)) { if (j - i > sum) sum = j - i; } } } System.out.println(sum); } } }