题解 | #公共子串计算#
公共子串计算
http://www.nowcoder.com/practice/98dc82c094e043ccb7e0570e5342dd1b
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String max1 = sc.nextLine();
String min1 = sc.nextLine();
System.out.println(getCom(max1,min1));
}
public static int getCom(String max,String min){
int count =0;
if(max.length()<min.length()){
String temp=max;
max=min;
min=temp;
for(int j=0;j<=min.length()-i;j++){
String com=min.substring(j,j+i);
if(max.contains(com) && com.length()>count){
count=com.length();
}
}
}
return count;
}
}
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String max1 = sc.nextLine();
String min1 = sc.nextLine();
System.out.println(getCom(max1,min1));
}
public static int getCom(String max,String min){
int count =0;
if(max.length()<min.length()){
String temp=max;
max=min;
min=temp;
}
//把短的字符串的子串全遍历一遍,然后看看长串包含那个子串,找包含最长的那个子串把结果返回出去
for(int i=1;i<=min.length();i++){for(int j=0;j<=min.length()-i;j++){
String com=min.substring(j,j+i);
if(max.contains(com) && com.length()>count){
count=com.length();
}
}
}
return count;
}
}