题解 | #公共子串计算#
公共子串计算
http://www.nowcoder.com/practice/98dc82c094e043ccb7e0570e5342dd1b
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String strA = sc.next();
String strB = sc.next();
int leng = 0;
if (strA.length() <= strB.length()) {
leng = getRes(strA, strB);
} else {
leng = getRes(strB, strA);
}
System.out.println(leng);
}
}
private static int getRes(String strA, String strB) {
int max = 0;
for (int i = 0; i < strA.length(); i++) {
for (int j = i + 1; j <= strA.length(); j++) {
if(strB.contains(strA.substring(i,j))){
max = Math.max(strA.substring(i, j).length(), max);
}
}
}
return max;
}
}