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 ...
随机推荐
- C语言字符串操作总结大全
1)字符串操作 strcpy(p, p1) 复制字符串 函数原型strncpy(p, p1, n) 复制指定长度字符串 函数原型strcat(p, p1) 附加字符串 函数原型strn ...
- 拉风的服务器监控工具,随时掌握服务器动态,AgileEAS.NET SOA 平台服务器监控工具集介绍
一.前言 AgileEAS.NET SOA 中间件平台是一款基于基于敏捷并行开发思想和Microsoft .Net构件(组件)开发技术而构建的一个快速开发应用平台.用于帮助中小型软件企业建立一条适合市 ...
- json-lib——JsonConfig详细使用说明
在使用json-lib包中JSONObject.fromObject(bean,cfg)时,可能出现以下几种情况: 1.(防止自包含)转换的对象包含自身对象,或者对象A下面挂了对象B,对象B下面又挂了 ...
- lua52 C API测试代码
//这是一篇lua与C++交互的情景测试 #include <lua.hpp> #include <lauxlib.h> #include <lualib.h> # ...
- linux 查看Java 进程的内存使用情况
top -b -n 1 | grep java| awk '{print "PID:"$1",mem:"$6",CPU percent:"$ ...
- C# 可视化读取文件、文件夹
OpenFileDialog fd = new OpenFileDialog(); fd.Filter = "txt files (*.txt)|*.txt|All files(*.*)|* ...
- sublime text3点击ctrl+B无法运行Python程序?
1.打开sublime text 3 ,选择 tools-->Build System-->New Build System.... 2.将下面代码块复制进新文件中,并命名为Python. ...
- Node.js-Socket.IO【1】-身份验证
Websocket身份验证失败的时候,希望向前台传输错误信息,但是Socket.IO目前最新版本1.4.6在后台使用 next(new Error('unauthorization')); 前端的代码 ...
- Only Link: What's the difference between dynamic dispatch and dynamic binding
http://stackoverflow.com/questions/20187587/what-is-the-difference-between-dynamic-dispatch-and-late ...
- 【Linux】unzip命令,记一次遇到的问题
最近在做BOSS系统云平台部署脚本,联调时发现Shell脚本中存在问题,下方记录 某个地方提示是否覆盖 [root@haiwai test]# unzip /home/redis/test/main- ...