题解 | #公共子串计算#
公共子串计算
http://www.nowcoder.com/practice/98dc82c094e043ccb7e0570e5342dd1b
import java.util.*;
public class Main{
public static void main(String [] args){
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String str1 = sc.nextLine();
String str2 = sc.nextLine();
int max = 0;
for (int i = 0; i < str1.length(); i++) {
int tmpMax = 0;
String s = String.valueOf(str1.charAt(i));
if (str2.contains(s)) {
int start = i;
int end = i + 1;
while (end <= str1.length() && str2.contains(str1.substring(start, end))) {
end++;
tmpMax++;
}
if (tmpMax > max) {
max = tmpMax;
}
}
}
System.out.println(max);
}
}
}
public class Main{
public static void main(String [] args){
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String str1 = sc.nextLine();
String str2 = sc.nextLine();
int max = 0;
for (int i = 0; i < str1.length(); i++) {
int tmpMax = 0;
String s = String.valueOf(str1.charAt(i));
if (str2.contains(s)) {
int start = i;
int end = i + 1;
while (end <= str1.length() && str2.contains(str1.substring(start, end))) {
end++;
tmpMax++;
}
if (tmpMax > max) {
max = tmpMax;
}
}
}
System.out.println(max);
}
}
}