题解 | #查找两个字符串a,b中的最长公共子串#
查找两个字符串a,b中的最长公共子串
https://www.nowcoder.com/practice/181a1a71c7574266ad07f9739f791506
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String s1 = in.nextLine();
String s2 = in.nextLine();
int max = 0;
if (s1.length() > s2.length()) {
String temp = s1;
s1 = s2;
s2 = temp;
}
String res = "";
for (int i = 0; i < s1.length(); i++) {
StringBuilder sb = new StringBuilder();
for (int j = i; j < s1.length(); j++) {
sb.append(s1.charAt(j));
if (s2.contains(sb.toString()) && sb.length() > max) {
max = sb.length();
res = sb.toString();
}
}
}
System.out.print(res);
}
}

