题解 | #公共子串计算#
公共子串计算
https://www.nowcoder.com/practice/98dc82c094e043ccb7e0570e5342dd1b
import java.util.Scanner;
// 穷举法
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()) { // 注意 while 处理多个 case
String a = in.nextLine();
String b = in.nextLine();
if (a.length() > b.length()) {
String t = b;
b = a;
a = t;
}
int max = 0;
for (int i = 0; i < b.length(); i++) {
for (int j = b.length(); j > i ; j--) {
String t = b.substring(i, j);
if (a.contains(t)) {
max = max < t.length() ? t.length() : max;
//算出最大的就可以break了
break;
}
}
}
System.out.println(max);
}
}
}