题解 | #公共子串计算#
公共子串计算
https://www.nowcoder.com/practice/98dc82c094e043ccb7e0570e5342dd1b
import java.util.*; // 注意类名必须为 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 big = a.length()>b.length()?a:b; String small = a.length()>b.length()?b:a; int max = 0; if(big.contains(small)){ System.out.println( small.length()); continue; } for(int i = 0 ;i <small.length()-1 ;i++){ for(int j = i+1 ; j<small.length() ;j++){ String res = small.substring(i,j); if(big.contains(res)){ max = Math.max(res.length(),max); } } } System.out.println( max ); } } }