题解 | #公共子串计算#双指针法
公共子串计算
https://www.nowcoder.com/practice/98dc82c094e043ccb7e0570e5342dd1b
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String s = in.nextLine();
String s1 = in.nextLine();
int result = 0;
for (int i = 0; i < s.length(); i++) {
String subStr = s.substring(i);
int j = 0;
int k = 0;
int l = 0;
while (j < subStr.length() && k < s1.length()) {
if (subStr.charAt(j) == s1.charAt(k)) {
j++;
k++;
l++;
result = Math.max(result, l);
} else {
j = j - l;
k = k - l + 1;
l = 0;
}
}
}
System.out.println(result);
}
}

查看22道真题和解析