最长公共子序列求序列模板提

Description:

描述:

This question has been featured in interview rounds of Amazon, MakeMyTrip, VMWare etc.

这个问题在亚马逊,MakeMyTrip,VMWare等访谈轮次中都有介绍。

Problem statement:

问题陈述:

Given two strings str1 and str2, find length of the longest common sub-sequence between them

给定两个字符串str1str2 ,找到它们之间最长的公共子序列的长度

    Let the strings be
str1="includehelp"
str2="letsinclude"
Output will be:
Longest common sub-sequence length is 7
The longest common sub-sequence is: "include"

The output is given above where the longest common sub-sequences is in same colour.

上面给出了最长公共子序列为相同颜色的输出。

Solution Approach:

解决方法:

The problem can be solved in a brute-force way. By generating all sub-sequences and checking them whether equal or not. Finally taking the longest common subsequence. But undoubtedly this is not at all computable since generating all sub-sequence is itself exponential and then permutations for checking any two sub-sequences.

这个问题可以用蛮力解决。 通过生成所有子序列并检查它们是否相等。 最后取最长的公共子序列。 但是毫无疑问,这根本不是可计算的,因为生成所有子序列本身就是指数的,然后进行排列以检查任意两个子序列。

The recursive way to solve is

递归的解决方法是

Let,

让,

        l1 = Length of the first string,str1
l2 = Length of the second string,str2
f(l1,l2) = Longest common subsequence length for string lengths l1 & l2

Now,

现在,

Think of the following example,

考虑以下示例,

Say first string is: x1 x2 ... xl1

假设第一个字符串是: x 1 x 2 ... x l 1

And the second string is: y1 y2 ... yl2

第二个字符串是: y 1 y 2 ... y l 2

Say,

说,

Then obviously we need to find LCS for the remaining part of string

and then add 1 for this character match

那么显然我们需要为字符串的其余部分找到LCS

Else

其他

Maximum of two case

最多两种情况

  1. LCS of the first string leaving character

    and second string

    第一个字符串离开字符的LCS

    和第二个字符串
  2. LCS of the first string

    and second string leaving character

    第一个字符串的LCS

    和第二个字符串离开字符

Now, we need to recur down to 0. So,

现在,我们需要递归降至0。因此,

Where base cases are,

在基本情况下,

If you generate this recursion tree, it will generate many overlapping sub-problems and thus, we need to reduce the re-computing. That’s why we need to convert it into dynamic programming where we will store the output of the sub-problems and we will use it to compute bigger sub-problems.

如果生成此递归树,它将生成许多重叠的子问题,因此,我们需要减少重新计算。 这就是为什么我们需要将其转换为动态编程,以便在其中存储子问题的输出,并使用它来计算更大的子问题。

转换为动态编程 (Converting to Dynamic programing)

    1)  Initialize dp[l1+1][l2+1]  to 0
2)  Convert the base case of recursion:
for i=0 to l1
dp[i][0]=0;
for i=0 to l2
dp[0][i]=0;
3)  Fill the DP table as per recursion.
for i=1 to l1    //i be the subproblem length for str1
for j=1 to l2 //j be the subproblem length for str2
if(str1[i-1]==str2[j-1]) //xl1==yl2
dp[i][j]=dp[i-1][j-1]+1;
else
dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
end for
end for
4)  The final output will be dp[l1][l2]

C++ Implementation:

C ++实现:

#include <bits/stdc++.h>
using namespace std;
int max(int a, int b)
{return (a > b) ? a : b;
}
int LCS(string str1, string str2)
{int l1 = str1.length();
int l2 = str2.length();
int dp[l1 + 1][l2 + 1];
for (int i = 0; i <= l1; i++)
dp[i][0] = 0;
for (int i = 0; i <= l2; i++)
dp[0][i] = 0;
for (int i = 1; i <= l1; i++) {for (int j = 1; j <= l2; j++) {if (str1[i - 1] == str2[j - 1])
dp[i][j] = dp[i - 1][j - 1] + 1;
else
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
}
}
return dp[l1][l2];
}
int main()
{string str1, str2;
cout << "Enter first string\n";
cin >> str1;
cout << "Enter Second string\n";
cin >> str2;
cout << "Longest Common sub-sequence length is: " << LCS(str1, str2) << endl;
return 0;
}

Output

输出量

Enter first string
includehelp
Enter Second string
letsincludeus
Longest Common sub-sequence length is: 7

翻译自: https://www.includehelp.com/icp/longest-common-subsequence.aspx

最长公共子序列求序列模板提

最长公共子序列求序列模板提_最长公共子序列相关推荐

  1. 怎么判断一个字符串的最长回文子串是否在头尾_最长回文字串/子序列问题(leetcode5,9,519)

    leetcode 5 最长回文子串 给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为 1000. 示例 1: 输入: "babad" 输出: " ...

  2. c语言统计最长单词长度,求3个字符串中最长单词的长度 求救 会一个的

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 这是我编的1个得 #include #include int main() {int alphabetic(char); int longest(char ...

  3. 计算机李长云,11月14日下午_李长云教授应邀作考研专题讲座_学术动态_湖南交通工程学院...

    本网讯(通讯员摄影李建华)为进一步激发同学们的考研热情,更好地解答有关研究生报考的相关问题,11月14日下午,学校特邀湖南工业大学研究生院院长李长云教授在学生食堂三楼学术报告厅作考研专题讲座.校长章怀 ...

  4. 【100题】 第四十七题 序列的最长递增、递减序列

    一,题目 求一个数组的最长递减子序列 比如{9,4,3,2,5,4,3,2}的最长递减子序列为{9,5,4,3,2} 求一个数组的最长递增子序列 比如{1,-1,2,-3,4,-5,6,-7}的最长递 ...

  5. c语言最长公共子序列,算法设计与分析/动态规划——最长公共子序列LCS及模板...

    这位大佬写的对理解DP也很有帮助,我就直接摘抄过来了,代码部分来自我做过的题 一,问题描述 给定两个字符串,求解这两个字符串的最长公共子序列(Longest Common Sequence).比如字符 ...

  6. 动态规划:求两个字符串的最长公共子序列

    问题描述:求两个字符串的最长公共子序列. 思路:使用动态规划的思想,将问题分解为小的子问题. 假设两个字符串序列分别为:X{x0, x1, x2,......, xm}, Y{y0, y1, y2,. ...

  7. 求序列最长不下降子序列_树状数组解决最长不下降子序列 讲讲主要思路就好...

    展开全部 不降子序列求的是一个元素的值单调e69da5e887aa62616964757a686964616f31333361306430不降的序列. 传统的状态设计便是使用f[n] 表示到达第n位时 ...

  8. 最长公共子上升序列(信息学奥赛一本通-T1306)

    [题目描述] 给定两个整数序列,写一个程序求它们的最长上升公共子序列. 当以下条件满足的时候,我们将长度N的序列S1,S2,...,SN 称为长度为M的序列A1,A2,...,AM的上升子序列: 存在 ...

  9. 求两个字符串的最长公共字串(连续)

    题目描述: 输入两个字符串,求其的最长的公共的字串,这与最长公共子序列不一样 输出两字符串的最长公共字串 思路一: 从字符串A开始遍历,同时遍历字符串A,找到第一个与当前字符串A相同的字符,此时记下当 ...

最新文章

  1. MSSQL2008R2 Failover Cluster(A-A)配置实验
  2. python django框架如何导出_python框架django的数据库的正向生成和反向生成
  3. 数学建模第三节2020.4.17-5.3补
  4. ASP.NET MVC @helper使用说明
  5. 怎么通过MQTT查看数据是否上云端_设备工程师们的福利来啦!JSON数据采集网关帮你实现云端对接~...
  6. oracle adg 备份,Oracle Physical Dataguard环境使用RMAN备份和恢复
  7. Flume Source
  8. 通用印刷体文字识别_印刷体文字识别(汉字)中文符的分割
  9. CAN FD的波特率到底能跑多快?
  10. powerbi中的合并
  11. 还在用 ZXing ? 试试华为统一扫码服务吧!
  12. android 天气动态壁纸,动态桌面壁纸 安卓“墨迹天气”新版评测
  13. Android UI开发:AlertDialog对话框
  14. 如何使用CH340G模块给51单片机下载程序
  15. IDEA 神级插件!效率提升 50 倍!
  16. day3----部署duboo微服务值部署zk和Jenkins(3)
  17. python datetime strftime_datetime.strftime时间输出转换
  18. h5物体拖动_网易爆款H5 交互玩法大合集(不看后悔系列)
  19. c语言欺凌,《中国校园欺凌调查报告》发布 语言欺凌占主导
  20. 逆波兰式的产生与计算

热门文章

  1. Bash脚本教程之基本语法
  2. mysql字段简索引_MySQL常用的一些语句,索引,字段等
  3. 云桌面 瘦终端_小米盒子连接Citrix云桌面
  4. js基本包装类型和引用类型
  5. scss-字符串连接符
  6. 调试windows服务
  7. 执行点击事件,第一次点击后,一切正常,第二次点击,执行两次,以此类推
  8. Iterator作用
  9. django QuerySet对象转换成字典对象
  10. SpringMVC搭建+实例