LintCode Longest Common Substring
原题链接在这里:http://www.lintcode.com/en/problem/longest-common-substring/#
题目:
Given two strings, find the longest common substring.
Return the length of it.
Given A = "ABCD", B = "CBCE", return 2. "BC"
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的更多相关文章
- Lintcode: Longest Common Substring 解题报告
Longest Common Substring 原题链接: http://lintcode.com/zh-cn/problem/longest-common-substring/# Given tw ...
- lintcode 77.Longest Common Subsequence(最长公共子序列)、79. Longest Common Substring(最长公共子串)
Longest Common Subsequence最长公共子序列: 每个dp位置表示的是第i.j个字母的最长公共子序列 class Solution { public: int findLength ...
- SPOJ LCS2 - Longest Common Substring II
LCS2 - Longest Common Substring II A string is finite sequence of characters over a non-empty finite ...
- Longest Common Substring
Given two strings, find the longest common substring. Return the length of it. Example Given A = &qu ...
- 【SPOJ】1812. Longest Common Substring II(后缀自动机)
http://www.spoj.com/problems/LCS2/ 发现了我原来对sam的理解的一个坑233 本题容易看出就是将所有匹配长度记录在状态上然后取min后再对所有状态取max. 但是不要 ...
- hdu 1403 Longest Common Substring(最长公共子字符串)(后缀数组)
http://acm.hdu.edu.cn/showproblem.php?pid=1403 Longest Common Substring Time Limit: 8000/4000 MS (Ja ...
- 后缀自动机(SAM):SPOJ Longest Common Substring II
Longest Common Substring II Time Limit: 2000ms Memory Limit: 262144KB A string is finite sequence of ...
- 后缀自动机(SAM) :SPOJ LCS - Longest Common Substring
LCS - Longest Common Substring no tags A string is finite sequence of characters over a non-empty f ...
- 后缀数组:HDU1043 Longest Common Substring
Longest Common Substring Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
随机推荐
- springBoot专题3---->springBoot与多数据源的配置
最近有点忙,更新有点慢.今天进来说说一说springBoot中如何配置多数据源. 第一,新建一个名为springBoot-mutidata的maven项目,完整的pom.xml配置如下: <?x ...
- 利用python的双向队列(Deque)数据结构实现回文检测的算法
#!/usr/bin/env python # -*- coding: utf-8 -*- # learn <<Problem Solving with Algorithms and Da ...
- 铁区MES部分页面展示
激活码: 76231722-2e7554593-b750-07e2f4844531 TIP: 若您激活不成功,可能是因为您所安装的软件版本较低,请尝试以下激活码 激活码: RXWY-A25421-K5 ...
- 【leetcode】Simplify Path
题目简述: Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/& ...
- ANdroid Studio查看debug SHA1
先打开 之后再命令行里输入cd.android 回车 在输入keytool -list -keystore debug.keystore 回车 之后会显示叫你输入密钥库口令: 这是你输入androi ...
- python 函数传递方式
在python中方法传递的参数到底是值传递还是引用传递? 1. 首先需要知道python中变量的类型:Python的变量分为可变变量和不可变变量. 针对于不可变的类型比如string int def ...
- Java -> 把Excel表格中的数据写入数据库与从数据库中读出到本地 (未完善)
写入:没有关闭流,容错并不完善. private void insertFile(HttpServletRequest request, HttpServletResponse response) t ...
- zorka源码解读之tracer内部实现
核心类: ZorkaAsyncThread.java protected BlockingQueue<T> submitQueue; /** * Processes single item ...
- 【Oracle】oracle取最大值和最小值的几个方法汇总
(1)oracle使用keep分析函数取最值记录 -- 取工资sal最大的雇员姓名及其工资,以及工资sal最少的雇员姓名及其工资 select deptno, empno, ename, sal, m ...
- 2016-2017 ACM-ICPC, NEERC, Moscow Subregional Contest
A. Altitude 从小到大加入每个数,用set查找前驱和后继即可. 时间复杂度$O(n\log n)$. #include <bits/stdc++.h> using namespa ...