原题链接在这里: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. 剪短的python数据结构和算法的书《Data Structures and Algorithms Using Python》

    按书上练习完,就可以知道日常的用处啦 #!/usr/bin/env python # -*- coding: utf-8 -*- # learn <<Problem Solving wit ...

  2. C# WebBrowser不能运行打开页面的activex

    如果是在64位操作系统中编译的代码,如果activex是32位的那么将不会运行32位的activex. 解决方法:右击项目->属性->生成->目标平台[x86]

  3. CodeForces 544A

    You are given a string q. A sequence of k strings s1, s2, ..., sk is called beautiful, if the concat ...

  4. POJ 2431Expedition

    Description A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Bein ...

  5. Codeforces Round #254 (Div. 2) A DZY Loves Chessboard

    先生成nXm的BW棋盘 BWBWBWBW WBWBWBWB BWBWBWBW WBWBWBWB 类似上面交替变换 然后将输入为’-’的地方替换成‘-’即可 #include <iostream& ...

  6. 【BZOJ】3523: [Poi2014]Bricks

    题意 \(n(n \le 1000000)\)个物品,颜色分别为\(a[i]\),现在要求排在一排使得相邻两个砖块的颜色不同,且限定第一个砖块和最后一个砖块的颜色,输出一个合法解否则输出-1. 分析 ...

  7. IIS运行.NET4.0配置

    IIS运行.NET4.0配置 “/CRM”应用程序中的服务器错误.配置错误说明: 在处理向该请求提供服务所需的配置文件时出错.请检查下面的特定错误详细信息并适当地修改配置文件. 分析器错误消息: 无法 ...

  8. HTML5 meta最全使用手册

    1.声明文档使用的字符编码 <meta charset='utf-8'> 2.声明文档的兼容模式 <meta http-equiv="X-UA-Compatible&quo ...

  9. React-Native android在windows下的踩坑记

    坑很多,跳之前做好准备.没有VPN的同学请浏览完本文后慎行.   你需要先安装最新版本的node.js(我最后使用的是v4.1.2),前往官网下载>> 注:我win7已经安装过Visual ...

  10. EmptyResultDataAccessException

    报此错误 :org.springframework.dao.EmptyResultDataAccessException: Incorrect result size: expected 1, act ...