题解 | #查找两个字符串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.hasNext()) { // 注意 while 处理多个 case String a = in.next(); String b = in.next(); String c ; String d; if (a.length() < b.length() ) { c = a; d = b; } else { c = b; d = a ; } // if (d.contains(c)) { // System.out.println(c); // continue; // } fir: for (int j = c.length() ; j > 0 ; j --) { sec : for (int i = 0 ; i <= c.length() - j; i++) { String res = c.substring(i, i + j); if (d.contains(res)) { System.out.println(res); break fir; } } } } } }