import java.util.Scanner;
/**
* 两个字符串最长公共子串
* @author Green.Gee
* @date 2022/12/1 14:07
* @email green.gee.lu@gmail.com
*/
public class MaxCommonSubStrInTwoStr {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextLine()) { // 注意 while 处理多个 case
String a = in.nextLine();
String b = in.nextLine();
if(a.length() > b.length()){
String temp = a;
a = b;
b = temp;
}
int i = 0;
int max = 0;
while(i < a.length()){
int j = i;
while(j <= a.length()){
String temp = a.substring(i,j);
if(max < temp.length() && b.contains(temp)){
max = temp.length();
}
j++;
}
i++;
}
System.out.println(max);
}
}
}
两个字符串最长公共子串 Java
最新推荐文章于 2024-04-15 13:15:54 发布