lintcode 77.Longest Common Subsequence(最长公共子序列)、79. Longest Common Substring(最长公共子串)
Longest Common Subsequence最长公共子序列:
每个dp位置表示的是第i、j个字母的最长公共子序列
class Solution {
public:
int findLength(vector<int>& A, vector<int>& B) {
int len1 = A.size();
int len2 = B.size();
if(len1 == || len2 == )
return ;
vector<vector<int>> result(len1+,vector<int>(len2+));
for(int i = ;i <= len1;i++){ //第一行第一列都为0,因为第一行第一列都没有字符串
result[i][] = ;
}
for(int i = ;i <= len2;i++){
result[][i] = ;
}
for(int i = ;i <= len1;i++){
for(int j = ;j <= len2;j++){
if(A[i-] == B[j-])
result[i][j] = result[i-][j-] + ;
else
result[i][j] = max(result[i-][j],result[i][j-]);
}
}
return result[len1][len2];
}
};
Longest Common Substring最长公共子串
每个dp代表以i、j这个坐标的最长公共子串,所以求最终的要遍历所有的
class Solution {
public:
int findLength(vector<int>& A, vector<int>& B) {
int len1 = A.size();
int len2 = B.size();
if(len1 == || len2 == )
return ;
vector<vector<int>> result(len1+,vector<int>(len2+));
for(int i = ;i <= len1;i++){ //第一行第一列都为0
result[i][] = ;
}
for(int i = ;i <= len2;i++){
result[][i] = ;
}
for(int i = ;i <= len1;i++){
for(int j = ;j <= len2;j++){
if(A[i-] == B[j-])
result[i][j] = result[i-][j-] + ;
else
result[i][j] = ;
}
}
int maxnum = 0x80000000;
for(int i = ;i <= len1;i++){
for(int j = ;j <= len2;j++){
if(result[i][j] > maxnum)
maxnum = result[i][j];
}
}
return maxnum;
}
};
更简洁的一种写法:
class Solution {
public:
/**
* @param A: A string
* @param B: A string
* @return: the length of the longest common substring.
*/
int longestCommonSubstring(string &A, string &B) {
// write your code here
int m = A.size();
int n = B.size();
vector<vector<int> > dp(m+,vector<int>(n+));
for(int i = ;i <= m;i++)
dp[i][] = ;
for(int i = ;i <= n;i++)
dp[][i] = ;
int max_num = ;
for(int i = ;i <= m;i++){
for(int j = ;j <= n;j++){
if(A[i-] == B[j-]){
dp[i][j] = dp[i-][j-] + ;
max_num = max(max_num,dp[i][j]);
}
else
dp[i][j] = ;
}
}
return max_num;
}
};
相同:都要建立m+1,n+1的二维数组
区别:1. 最长公共子串要求连续的,最长公共子序列可以不连续,所以dp的递推公式第二项不同
2. 最长公共子序列最后一个值就是最长,最长公共子串要比较哪个位置最长
lintcode 77.Longest Common Subsequence(最长公共子序列)、79. Longest Common Substring(最长公共子串)的更多相关文章
- 动态规划求最长公共子序列(Longest Common Subsequence, LCS)
1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...
- 动态规划之最长公共子序列LCS(Longest Common Subsequence)
一.问题描述 由于最长公共子序列LCS是一个比较经典的问题,主要是采用动态规划(DP)算法去实现,理论方面的讲述也非常详尽,本文重点是程序的实现部分,所以理论方面的解释主要看这篇博客:http://b ...
- 动态规划 ---- 最长公共子序列(Longest Common Subsequence, LCS)
分析: 完整代码: // 最长公共子序列 #include <stdio.h> #include <algorithm> using namespace std; ; char ...
- (最长公共子序列 暴力) Common Subsequence (poj 1458)
http://poj.org/problem?id=1458 Description A subsequence of a given sequence is the given sequence w ...
- 最长上升子序列 LIS(Longest Increasing Subsequence)
引出: 问题描述:给出一个序列a1,a2,a3,a4,a5,a6,a7….an,求它的一个子序列(设为s1,s2,…sn),使得这个子序列满足这样的性质,s1<s2<s3<…< ...
- C#LeetCode刷题之#594-最长和谐子序列(Longest Harmonious Subsequence)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3800 访问. 和谐数组是指一个数组里元素的最大值和最小值之间的差 ...
- 最长上升子序列(Longest increasing subsequence)
问题描述 对于一串数A={a1a2a3…an},它的子序列为S={s1s2s3…sn},满足{s1<s2<s3<…<sm}.求A的最长子序列的长度. 动态规划法 ...
- LeetCode 300. 最长上升子序列(Longest Increasing Subsequence)
题目描述 给出一个无序的整形数组,找到最长上升子序列的长度. 例如, 给出 [10, 9, 2, 5, 3, 7, 101, 18], 最长的上升子序列是 [2, 3, 7, 101],因此它的长度是 ...
- 最长公共子序列(LCS)和最长递增子序列(LIS)的求解
一.最长公共子序列 经典的动态规划问题,大概的陈述如下: 给定两个序列a1,a2,a3,a4,a5,a6......和b1,b2,b3,b4,b5,b6.......,要求这样的序列使得c同时是这两个 ...
- 最长公共子序列(LCS)、最长递增子序列(LIS)、最长递增公共子序列(LICS)
最长公共子序列(LCS) [问题] 求两字符序列的最长公共字符子序列 问题描述:字符序列的子序列是指从给定字符序列中随意地(不一定连续)去掉若干个字符(可能一个也不去掉)后所形成的字符序列.令给定的字 ...
随机推荐
- 14-----BBS论坛
BBS论坛(十四) 14.1注册完成跳到上一个页面 (1)front/form.py # front/forms.py __author__ = 'derek' from ..forms import ...
- 安装eclipse for ee
去官网下载最新版本版本的linux版本的eclipse for ee,下载到Downloads文件夹. 解压文件夹 sudo tar -zxvf eclipse-jee-2018-09-linux-g ...
- JS你可能还不知道的一些知识点(一)
js程序是用Unicode字符集编写的, 2.转义字符:反斜线 1 2 3 4 function Test(){ var s='you\'re right,it can\'t be a quote ...
- python 实现连接mysql并读一条数据写到csv一条数据
import MySQLdb as mdb import csv with open('my.csv', 'w+', newline='') as csv_file: writer = csv.wri ...
- STlink及烧写工具:
一 STLINK可以随时在STLINK与jlink之间切换:方法:https://www.segger.com/products/debug-probes/j-link/models/other-j- ...
- 软件使用---Eclipse
代码提示快捷操作.这个叫做,内容分析(content assist) 1.设置自动提示: 2.设置快捷键:
- 我使用的brackets插件
livereload atom dark theme autoprefixer auto save files on window blur beautify brackets file icons ...
- Linux网卡操作
单个网卡操作 [root@localhost ~]# ifdown eth0 #关闭网络 [root@localhost ~]# ifup eth0 #启动网络 网络服务: [root@localho ...
- stm32 外部中断学习
今天我们看看STM32的外部中断实验. STM32 供 IO 口使用的中断线只有 16 个,但是 STM32 的 IO 口却远远不止 16 个,那么 STM32 是怎么把 16 个中断线和 IO 口一 ...
- 换晶振导致stm32串口数据飞码的解决办法(补充)
今天(2014.4.21)把stm32f107的程序下载到stm32f103的板子上,发现串口收不到数据,突然想起晶振频率没有修改,#define HSE_VALUE ((uint32_t)13 ...