题解 | #公共子串计算#
公共子串计算
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 a = in.nextLine();
String b = in.nextLine();
if (a.length() > b.length()) {
String tmp = a;
a = b;
b = tmp;
}
for (int i = a.length(); i > 0; i--) {
int offset = 0;
String temp = getNextStr(i,offset,a);
while (temp != null) {
if (b.contains(temp)) {
System.out.println(temp.length());
return;
}
temp = getNextStr(i, offset++, a);
}
}
System.out.println(0);
}
static String getNextStr(int i, int offset,String a) {
try {
return a.substring(offset, i + offset);
} catch (Exception e) {
return null;
}
}
}
