[Algorithms] Longest Common Substring
The Longest Common Substring (LCS) problem is as follows:
Given two strings s and t, find the length of the longest string r, which is a substring of both s and t.
This problem is a classic application of Dynamic Programming. Let's define the sub-problem (state) P[i][j] to be the length of the longest substring ends at i of s and j of t. Then the state equations are
- P[i][j] = 0 if s[i] != t[j];
- P[i][j] = P[i - 1][j - 1] + 1 if s[i] == t[j].
This algorithm gives the length of the longest common substring. If we want the substring itself, we simply find the largest P[i][j] and return s.substr(i - P[i][j] + 1, P[i][j]) or t.substr(j - P[i][j] + 1, P[i][j]).
Then we have the following code.
string longestCommonSubstring(string s, string t) {
int m = s.length(), n = t.length();
vector<vector<int> > dp(m, vector<int> (n, ));
int start = , len = ;
for (int i = ; i < m; i++) {
for (int j = ; j < n; j++) {
if (i == || j == ) dp[i][j] = (s[i] == t[j]);
else dp[i][j] = (s[i] == t[j] ? dp[i - ][j - ] + : );
if (dp[i][j] > len) {
len = dp[i][j];
start = i - len + ;
}
}
}
return s.substr(start, len);
}
The above code costs O(m*n) time complexity and O(m*n) space complexity. In fact, it can be optimized to O(min(m, n)) space complexity. The observations is that each time we update dp[i][j], we only need dp[i - 1][j - 1], which is simply the value of the above grid before updates.
Now we will have the following code.
string longestCommonSubstringSpaceEfficient(string s, string t) {
int m = s.length(), n = t.length();
vector<int> cur(m, );
int start = , len = , pre = ;
for (int j = ; j < n; j++) {
for (int i = ; i < m; i++) {
int temp = cur[i];
cur[i] = (s[i] == t[j] ? pre + : );
if (cur[i] > len) {
len = cur[i];
start = i - len + ;
}
pre = temp;
}
}
return s.substr(start, len);
}
In fact, the code above is of O(m) space complexity. You may choose the small size for cur and repeat the same code using if..else.. to save more spaces :)
[Algorithms] Longest Common Substring的更多相关文章
- SPOJ LCS2 - Longest Common Substring II
LCS2 - Longest Common Substring II A string is finite sequence of characters over a non-empty finite ...
- LintCode Longest Common Substring
原题链接在这里:http://www.lintcode.com/en/problem/longest-common-substring/# 题目: Given two strings, find th ...
- 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 ...
- Longest Common Substring(最长公共子序列)
Longest Common Substring Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Ot ...
随机推荐
- linux下各种形式的shell加法操作总结
linux 下shell加法操作总结: #!/bin/bash n=1;echo -n "$n " let "n = $n + 1" echo -n & ...
- NoSQL(四)
mongodb介绍 https://www.yiibai.com/mongodb/mongodb_drop_collection.html 1.文档性数据库类似于json对象,分布式 mongodb安 ...
- unity, 在surface shader中访问顶点色
//ref: Custom data computed per-vertex: http://docs.unity3d.com/Manual/SL-SurfaceShaderExamples.html ...
- 理解x64代码模型
原作者:Eli Bendersky http://eli.thegreenplace.net/2012/01/03/understanding-the-x64-code-models 在编写x64架构 ...
- 并发insert情况下会发生重复的数据插入问题
1.背景 用多线程接收推送的订单数据,把接收的订单数据存到一个表中,实现的需求是:如果接收的订单消息在数据库中已经存在,那么执行update操作:如果没有存在,那么执行insert操作代码逻辑: if ...
- Powershell对象条件查询筛选
在 Windows PowerShell 中,与所需的对象数量相比,通常生成的对象数量以及要传递给管道的对象数量要多得多.可以使用 Format cmdlet 来指定要显示的特定对象的属性,但这并不能 ...
- Makefile 多个目标匹配的问题
在windows下直接使用mingw32-make # ZTHREAD_A the static link library file of ZThread ZTHREAD_A = F:/ZJ/tool ...
- Unity3D 5中增加WebGL 播放插件
http://www.csdn.net/article/2014-03-18/2818822-Unity-5-game-engine 其实我是搞3d的,这篇文章里所有的术语看了都有很强的亲切感. Un ...
- 如何使用VMWare共享Win7中的文件夹,对应Linux中的哪个目录下面?
访问 /mnt/hgfs/你设置的共享名,如果找不到这个hgfs这个文件夹,那说明你还没正确安装好 install VMware tools
- CC1101 433无线模块,STM8串口透传
CC1101 433无线模块,STM8串口透传 原理图:http://download.csdn.net/detail/cp1300/7496509 下面是STM8程序 CC1101.C /*** ...