Longest Common Substring

原题链接: http://lintcode.com/zh-cn/problem/longest-common-substring/#

Given two strings, find the longest common substring.

Return the length of it.

注意

The characters in substring should occur continiously in original string. This is different with subsequnce.

样例

 

标签 Expand

 

SOLUTION 1:

DP:
D[i][j] 定义为:两个string的前i个和前j个字符串,尾部连到最后的最长子串。
D[i][j] = 
1. i = 0 || j = 0 : 0
2. s1.char[i - 1] = s2.char[j - 1] ? D[i-1][j-1] + 1 : 0;
另外,创建一个max的缓存,不段更新即可。
 public class Solution {
/**
* @param A, B: Two string.
* @return: the length of the longest common substring.
*/
public int longestCommonSubstring(String A, String B) {
// write your code here
if (A == null || B == null) {
return 0;
} int lenA = A.length();
int lenB = B.length(); // bug 1: use error init.
int[][] D = new int[lenA + 1][lenB + 1]; int max = 0; // BUG 2: should use <= instead of <
for (int i = 0; i <= lenA; i++) {
for (int j = 0; j <= lenB; j++) {
if (i == 0 || j == 0) {
D[i][j] = 0;
} else {
if (A.charAt(i - 1) == B.charAt(j - 1)) {
D[i][j] = D[i - 1][j - 1] + 1;
} else {
D[i][j] = 0;
}
} max = Math.max(max, D[i][j]);
}
} return max;
}
}

GITHUB:

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/lintcode/dp/LongestCommonSubstring.java

Lintcode: Longest Common Substring 解题报告的更多相关文章

  1. Lintcode:Longest Common Subsequence 解题报告

    Longest Common Subsequence 原题链接:http://lintcode.com/zh-cn/problem/longest-common-subsequence/ Given ...

  2. LintCode Longest Common Substring

    原题链接在这里:http://www.lintcode.com/en/problem/longest-common-substring/# 题目: Given two strings, find th ...

  3. LeetCode: Longest Common Prefix 解题报告

    Longest Common Prefix Write a function to find the longest common prefix string amongst an array of ...

  4. Leetcode:【DP】Longest Palindromic Substring 解题报告

    Longest Palindromic Substring -- HARD 级别 Question SolutionGiven a string S, find the longest palindr ...

  5. 【LeetCode】Longest Palindromic Substring 解题报告

    DP.KMP什么的都太高大上了.自己想了个朴素的遍历方法. [题目] Given a string S, find the longest palindromic substring in S. Yo ...

  6. lintcode 77.Longest Common Subsequence(最长公共子序列)、79. Longest Common Substring(最长公共子串)

    Longest Common Subsequence最长公共子序列: 每个dp位置表示的是第i.j个字母的最长公共子序列 class Solution { public: int findLength ...

  7. SPOJ LCS2 - Longest Common Substring II

    LCS2 - Longest Common Substring II A string is finite sequence of characters over a non-empty finite ...

  8. Longest Common Substring

    Given two strings, find the longest common substring. Return the length of it. Example Given A = &qu ...

  9. 【SPOJ】1812. Longest Common Substring II(后缀自动机)

    http://www.spoj.com/problems/LCS2/ 发现了我原来对sam的理解的一个坑233 本题容易看出就是将所有匹配长度记录在状态上然后取min后再对所有状态取max. 但是不要 ...

随机推荐

  1. 【LeetCode】224. Basic Calculator

    Basic Calculator Implement a basic calculator to evaluate a simple expression string. The expression ...

  2. Hyperscan 介绍与安装【转】

    来源:http://blog.sina.com.cn/s/blog_913a533b0102wc38.html Hyperscan 介绍与安装 (2016-01-27 16:22:32) 转载▼   ...

  3. 用ElasticSearch存储日志

    介绍 如果你使用elasticsearch来存储你的日志,本文给你提供一些做法和建议. 如果你想从多台主机向elasticsearch汇集日志,你有以下多种选择: Graylog2 安装在一台中心机上 ...

  4. 【Spring】基于SpringMVC的图片验证码功能实现

    后台实现代码: ImgController.java 文件 package cn.shop.controller; import java.awt.Color; import java.awt.Fon ...

  5. 【jsp】配置错误页面

    1,使用JSP方式 如果配置是Jsp时,需要把isErrorPage设置为true, 以及设置 <%@ page language="Java" contentType=&q ...

  6. Oracle 12C -- 预定义audit policies

    在12C中,预定义了三种审计策略:ora_secureconfig,ora_database_parameter,ora_account_mgmt可以通过脚本$ORACLE_HOME/rdbms/ad ...

  7. 文件处理-智能检测编码的工具(chardet)

    一.chardet使用方法 问:假如你不知道你要处理的文件是什么编码可怎么办呢? import chardet f = open('通讯录.txt',mode='rb') data = f.read( ...

  8. [转]@PathVariable和@RequestParam的区别

    请求路径上有个id的变量值,可以通过@PathVariable来获取  @RequestMapping(value = "/page/{id}", method = Request ...

  9. [转]MVC 框架教程

    Spring web MVC 框架提供了模型-视图-控制的体系结构和可以用来开发灵活.松散耦合的 web 应用程序的组件.MVC 模式导致了应用程序的不同方面(输入逻辑.业务逻辑和 UI 逻辑)的分离 ...

  10. mysql 8.0给数据库添加用户和赋权

    -- 使用mysql 数据库 USE mysql -- 为mysql创建用户:case_dev 密码为:pass123 CREATE USER case_dev IDENTIFIED BY 'pass ...