题解 | #查找两个字符串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);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextLine()) { // 注意 while 处理多个 case
String s1 = in.nextLine();
String s2 = in.nextLine();
String ds = s1.length()>s2.length()?s2:s1;
String cs = s1.length()>s2.length()?s1:s2;
String rs = "";
for(int j=ds.length();j>=2;j--){
for(int i=0;i<=ds.length()-j;i++){
String sub = ds.substring(i,i+j);
if(cs.indexOf(sub)!=-1){
rs = sub;
break;
}
}
if(!rs.equals("")) break;
}
System.out.println(rs);
}
}
}
