lintcode -- 最长公共子串
来源:互联网 发布:华为程序员工资多少 编辑:程序博客网 时间:2024/05/23 02:00
给出两个字符串,找到最长公共子串,并返回其长度。
注意事项
子串的字符应该连续的出现在原字符串中,这与子序列有所不同。
给出A=“ABCD”,B=“CBCE”,返回 2
// 严格的说,这个算法是递推,而不是动态规划,但是可以用动态规划的四要素去分析换个解答。
// 为什么不是动态规划?因为最暴力的方式也就是 O(n^3) 可以找到A所有的Substring然后看看在不在B里。
public class Solution {
public int longestCommonSubstring(String A,String B){
int alen =A.length();
int blen =B.length();
int[][] dp = new int[alen+1][blen+1];
int max =0;
for(int i =1;i<=alen;i++){
for(int j=1;j<=blen;j++){
if(A.charAt(i-1) == B.charAt(j-1)){
dp[i][j] = dp[i-1][j-1] +1;
max = Math.max(max,dp[i][j]);
}else{
dp[i][j] =0;
}
}
}
return max;
}
}
- LintCode-最长公共子串
- LintCode 最长公共子串
- LintCode : 最长公共子串
- LintCode : 最长公共子串
- LintCode 最长公共子串
- 最长公共子串-LintCode
- lintcode -- 最长公共子串
- Lintcode(5)-最长公共子串
- lintcode-最长公共子串-79
- lintcode 求最长公共子串
- lintcode 之 最长公共子串
- LintCode 79 最长公共子串
- LintCode : 最长公共子序列
- LintCode 最长公共子序列
- 最长公共子序列-LintCode
- lintcode--最长公共子序列
- [Lintcode]Longest Common Substring最长公共子串
- Lintcode—(5)最长公共子串
- 免费空间(免备案,无广告) 1G免费全能空间
- 有关多态的那点事丶
- angular.js 指令集实现模态框拖动(3)
- 线程死锁检测方法
- 联合体了解
- lintcode -- 最长公共子串
- SSIS项目基础实战操作手册_v1.0(傻瓜版)(三)
- java实现动态规划求解给定矩阵的和最大的子数组(矩阵中数字正负均存在)
- 日期转毫秒数
- 虚拟化VMware vsphere
- Notepad++ 使用二进制或16进制模式查看文件
- 2017 Multi-University Training Contest
- Mac下MAMP Pro 启动数据库MySQL出错导致无法启动的问题
- Photon Server的使用(一)