原题链接在这里:http://www.lintcode.com/en/problem/longest-common-substring/#

题目:

Given two strings, find the longest common substring.

Return the length of it.

The characters in substring should occur continuously in original string. This is different with subsequence.
Example

Given A = "ABCD", B = "CBCE", return 2. "BC"

Challenge

O(n x m) time and memory.

题解:

Longest Common Subsequence相似.

DP. 参考http://www.geeksforgeeks.org/longest-common-substring/

dp[i][j]表示长度为i的str1 和 长度为j的str2 longest common substring长度.

看两个substring 的 suffix.

dp[i][j] = dp[i-1][j-1] + 1 if str1.charAt(i-1) == str2.charAt(j-1).

dp[i][j] = 0. o.w

Time Complexity: O(m*n). Space: O(m*n).

AC Java:

 public class Solution {
/**
* @param A, B: Two string.
* @return: the length of the longest common substring.
*/
public int longestCommonSubstring(String A, String B) {
if(A == null || B == null){
return 0;
}
int m = A.length();
int n = B.length();
int [][] dp = new int[m+1][n+1];
int longest = 0;
for(int i = 1; i<=m; i++){
for(int j = 1; j<=n; j++){
if(A.charAt(i-1) == B.charAt(j-1)){
dp[i][j] = dp[i-1][j-1]+1;
}else{
dp[i][j] = 0;
}
longest = Math.max(longest, dp[i][j]);
}
}
return longest;
}
}

LintCode Longest Common Substring的更多相关文章

  1. Lintcode: Longest Common Substring 解题报告

    Longest Common Substring 原题链接: http://lintcode.com/zh-cn/problem/longest-common-substring/# Given tw ...

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

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

  3. SPOJ LCS2 - Longest Common Substring II

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

  4. Longest Common Substring

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

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

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

  6. hdu 1403 Longest Common Substring(最长公共子字符串)(后缀数组)

    http://acm.hdu.edu.cn/showproblem.php?pid=1403 Longest Common Substring Time Limit: 8000/4000 MS (Ja ...

  7. 后缀自动机(SAM):SPOJ Longest Common Substring II

    Longest Common Substring II Time Limit: 2000ms Memory Limit: 262144KB A string is finite sequence of ...

  8. 后缀自动机(SAM) :SPOJ LCS - Longest Common Substring

    LCS - Longest Common Substring no tags  A string is finite sequence of characters over a non-empty f ...

  9. 后缀数组:HDU1043 Longest Common Substring

    Longest Common Substring Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

随机推荐

  1. UE移植到SAE云平台

    应用架在新浪的SAE上,而同时功能中又需要用上编辑器,鉴于百度的UEditor功能强大,可定制,文档全,所以理所当然的用它.而新浪把本地文件的IO操作禁止了,使得UEdiotr的图片上传.附件和在线涂 ...

  2. ldd 命令用于判断某个可执行的binary档案含有什么动态链接库(so)

    [root@NB ok]# ldd /bin/ls linux-vdso.so. => (0x00007ffd7dbf6000) libselinux.so. => /lib64/libs ...

  3. 卸载 ibus 使Ubuntu16.04任务栏与启动器消失 问题解决

    经查证是unity误卸载了,我使用了命令: sudo apt-get remove --purge ibus 解决方法是: 使用以下命令:重置compiz: dconf reset -f /org/c ...

  4. [译]:Orchard入门——给网站添加新博客

    原文链接:Adding a Blog to Your Site 文章内容基于Orchard 1.8版本 Orchard提供一个博客引擎--这让添加一个新博客到你网站变得非常容易. 本文将介绍怎样添加一 ...

  5. iOS中获取各种文件的目录路径的方法

    我们的app在手机中存放的路径是:/var/mobile/Applications/4434-4453A-B453-4ADF535345ADAF344 后面的目录4434-4453A-B453-4AD ...

  6. iTextSharp简单生成pdf和操作pdf添加水印

    遇到需要导出页面到pdf,并添加pdf水印的业务.稍微研究了下,借阅网友的前车之鉴,经过使用可行之后的代码贴出来,做个记录,也供需要的网友借阅. public class PDFSetWaterMar ...

  7. hdu2196 树形dp

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2196 Problem Description A school bought the fi ...

  8. 服务器&浏览器伪装的故事

    今天要说的是伪装,为嘛要伪装呢?我想,首先是心虚,不够自信,比如你安全措施做的不够,你怕别人黑你的系统,所以就要伪装.其次呢,我想就是有不可告人的秘密了,比如你有竞争对手总是找你的茬,拦截你,那咋办呢 ...

  9. T-SQL Recipes之生成动态列表数据

    Problem 首先什么是动态列表?举个示例,假设你想输出以逗号分隔的IDs,如: 1,45,67,199,298 Solution 生成动态列表数据在我们生活场景中很常见,比如在 Dynamic P ...

  10. Leetcode Valid Sudoku

    Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...