版权声明:071623 https://blog.csdn.net/weixin_43584220/article/details/88888011
package cn.LanQiaoBeiAlgorithm.Ravanla;
/**
* 求两个字符串的最大公共子串
* @author Ravanlala
* abc的子序列为 ab bc ba cb ca
* 子串:不间隔的
* 子序列:可以间隔的序列
*/
public class Common_Sub_String {
public static void main(String[] args) {
int k = f("abd", "bacfkd");
System.out.println(k);
/**
* 假如两个字符数组中只由两个字符组成
* 我们可以这样去想
* 我们只比较两数组的第一个
* 相同则加1,再递归下一个,并把第一个位置删掉
* 不相同则判断两种情况谁最大,第一种是s1数组减一了跟s2比较
* 第二种是s2数组减一了根s1比较
* 哪种情况最大就取哪种
* 这样一来只要判断像两个字符组成的数组就可以了,之后的交给递归去判断
*/
}
private static int f(String string, String string2) {
if(string.length() == 0 || string2.length() == 0) {
return 0;
}
if(string.charAt(0) == string2.charAt(0)) {
return f(string.substring(1), string.substring(1)) + 1;
}
else {
return Math.max(f(string.substring(1), string2), f(string, string2.substring(1)));
//f方法里的位置还不能调换了
}
}
}