题解 | #公共子串计算#
公共子串计算
http://www.nowcoder.com/practice/98dc82c094e043ccb7e0570e5342dd1b
30,10等下试试滑动窗口
import java.math.*;
//import java.lang.*;
public class Main{
public static void main(String[]args){
Scanner input = new Scanner(System.in);
String str1 = input.nextLine();
String str2 = input.nextLine();
//String str1 = "baaabbabababbaabbaaaaaabbaabaabaaabbabbbbaabbaaabbbbbabababaaaaabbbbbabbbabaababaa";
//String str2 = "bbbbbababbabaabbaabba";
int line1 = str1.length();
int lesline = 0;
int linelength = 0;
int line2 = str2.length();
if(line1 > line2){
lesline = line2;
}else{
lesline = line1;
}
//分长短是因为长串会经历很多次无用分割
//知道短长度之后,对比期间不用区分长短,因为对比是相互的
for(int i = lesline; i > 0; i--){ //指定字符串长度
//int flag1 = 1;
String a="";
for(int ii = 0; ii <= line1; ii++){ //指定字符首所在位置
if(ii+i > line1){ //ii+i > lesline ||
break;
}
a = str1.substring(ii,ii+i);
if((str2.indexOf(a) != -1) && (i >= linelength)){
System.out.println(i);
linelength = i;
break;
}
}
if((str2.indexOf(a)!=-1) && (i >= linelength)){
break;
}
}
//System.out.println(linelength);
if(linelength == 0){
System.out.println(linelength);
}
}
}