[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 ...
随机推荐
- centos 7 生成文件名乱码的问题如何解决?
在应用脚本生成文件时,发现生成的文件名称出现乱码(似麻将一样).刚开始找来找去,以为是复制粘贴的原因,复制时复制了特殊字符导致的,结果修改源文件后发现生成的文件名还是乱码.后来在执行脚本时,提示&qu ...
- Service stopSelf(int statId)和onStartcommand(Intent intent,int flags,int startId)
Stopping a service A started service must manage its own lifecycle. That is, the system does not sto ...
- Linux上寻找并杀死僵尸进程
转载: http://blog.csdn.net/shanzhizi/article/details/47320595 linux服务器上,多少会出现一些僵尸进程,下面介绍如何快速寻找和消灭这些僵尸进 ...
- Lintcode---线段树查询(区间最大值)
对于一个有n个数的整数数组,在对应的线段树中, 根节点所代表的区间为0-n-1, 每个节点有一个额外的属性max,值为该节点所代表的数组区间start到end内的最大值. 为SegmentTree设计 ...
- 远程调试 Weinre
什么是远程调试? 说白了,就是可以通过PC端[F12开发者工具]查看并调试移动端的页面内容,如html.css.js.Network资源等. 重要的事情说三遍:weinre所占有的端口不需要和监听页面 ...
- Android 中查看内存的使用情况集经常使用adb命令
1. 在IDE中查看Log信息 当程序执行垃圾回收的时候,会打印一条Log信息.其格式例如以下: D/dalvikvm: <GC_Reason> <Amount_freed>, ...
- NGUI 取ScrollView中遮罩区域4个点
用panel.localCorners而不是panel.finalClipRegion,Region还要再换算 首先通过ScrollView取panel,然后取Corners,它返回值代表4个点,映射 ...
- Unix删除当前目录可执行文件
On GNU versions of find you can use -executable: find . -type f -executable -printFor BSD versions o ...
- 自定义闹钟 Reminder
Reminder reminder = ScheduledActionService.Find("MY REMINDER") as Reminder; if ( reminder ...
- Yii2中对数据库的查询方法如下
User::find()->all(); 此方法返回所有数据: User::findOne($id); 此方法返回 主键 id=1 的一条数据(举个例子): User::find()->w ...