一、题目描述
求两个字符串s1和s2的最长公共子串。
示例:
输入:s1 = "photograph" s2 = "tomography"
输出:ograph
二、解题思路
其实本题就是字符串编辑距离的变种,字符串编辑距离会了,此题就会了。本题跟最长公共子序列也是非常像。详见动态规划经典题目-最长公共子序列
1. 定义状态
设dp[i][j]表示字符串s1的前i个字符与字符串s2前j个字符的最长公共子串的长度。
2. 定义状态转移方程
当s1[i] == s2[j]时,有 d p [ i ] [ j ] = d p [ i − 1 ] [ j − 1 ] + 1 dp[i][j] = dp[i-1][j-1] + 1 dp[i][j]=dp[i−1][j−1]+1
否则 d p [ i ] [ j ] = 0 dp[i][j] = 0 dp[i][j]=0
3. 初始化
显然dp[0][j]初始化为0。
显然dp[i][0]初始化为0。
三、代码实现
/**
* 求两个字符串的最长公公子串
*
*
* @author hh
* @date 2021-5-16 23:08
*/
public class LongestCommonSubString {
public String longestCommonSubString(String s1,String s2){
int[][] dp = new int[s1.length() + 1][s2.length() + 1];
//初始化行
for(int i = 0 ; i <= s2.length(); i++){
dp[0][i] = 0;
}
//初始化列
for(int i = 0; i <= s1.length(); i++){
dp[i][0] = 0;
}
//计算dp数组
for(int i = 1; i <= s1.length(); i++){
for(int j = 1; j <= s2.length(); j++){
if(s1.charAt(i - 1) == s2.charAt(j -1 )){
dp[i][j] = dp[i-1][j-1] + 1;
}else{
dp[i][j] = 0;
}
}
}
//匹配的最长子串长度
int maxLength = Integer.MIN_VALUE;
int index = 0;
//检测是否匹配
for(int i = 0; i <= s2.length(); i++){
if(dp[s1.length()][i] > maxLength){
maxLength = dp[s1.length()][i];
index = i;
}
}
return s2.substring(index - maxLength,index);
}
public static void main(String[] args){
LongestCommonSubString longestCommonSubString = new LongestCommonSubString();
String s1 = "photograph";
String s2 = "tomography";
System.out.println(longestCommonSubString.longestCommonSubString(s1,s2));
}
}