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

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

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"

Longest Common Subsequence

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。因此,

Longest Common Subsequence

Where base cases are,

在基本情况下,

Longest Common Subsequence

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

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/377334.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

洛必达法则使用条件

使用条件 1、分子分母同趋向于0或无穷大 。 2、分子分母在限定的区域内是否分别可导。 3、当两个条件都满足时&#xff0c;再求导并判断求导之后的极限是否存在&#xff1a;若存在&#xff0c;直接得到答案&#xff1b;若不存在&#xff0c;则说明此种未定式无法用洛必达法则解…

求根号m(巴比伦算法)

巴比伦算法是针对求根号m的近似值情况的&#xff0c;它的思想是这样的&#xff1a; 设根号mX0,则如果枚举有答案X(X<X0)&#xff0c;则m/X>X0,当精度要求不高的时候&#xff0c;我们可以看成Xm/XX0,而如果精度要求比较高&#xff0c;我们只需取X和m/X的平均值作为新的枚举…

Android面试题

http://blog.csdn.net/aomandeshangxiao/article/category/841452 http://www.cppblog.com/life02/category/18316.html转载于:https://www.cnblogs.com/DonkeyTomy/articles/2598673.html

r语言 分类变量 虚拟变量_R语言中的变量

r语言 分类变量 虚拟变量R语言| 变数 (R Language | Variables) In the previous tutorial, we have come across the basic information that stands as a pavement for understanding the R language in depth. Now moving future let us educate ourselves about the concep…

算法题复习(快排、链表、二分、哈希、双指针)

目录1、快速排序复习2、链表部分复习203. 移除链表元素707. 设计链表206. 反转链表142.环形链表 II3、二分法复习4、哈希法复习5、双指针复习**15. 三数之和****18. 四数之和****27. 移除元素****344. 反转字符串**,简单&#xff0c;双指针从两侧往中间靠拢&#xff0c;并随时s…

Cassandra1.2文档学习(7)—— 规划集群部署

数据参考&#xff1a;http://www.datastax.com/documentation/cassandra/1.2/webhelp/index.html#cassandra/architecture/architecturePlanningAbout_c.html 当规划一个Cassandra集群部署时&#xff0c;关于你初始存储的数据的数据量你应当有一个好的想法&#xff0c;并且对于…

虚拟机设置NAT

需要开启虚拟机网络相关服务&#xff0c; 安装虚拟网卡&#xff0c; 还有必须安装 VMware ToolsVMware虚拟机下实现NAT方式上网1. 把你的虚拟网卡VMnet8设置为自动获得IP、自动获得DNS服务器&#xff0c;启用。2. 把你虚拟机中操作系统的“本地连接”也设置为自动获得IP、自动获…

窗体震动 C# (不使用Timer控件,控制窗体震动)

private static Point plocation new Point(); public static void StartVibration(Form form)//Form 传入需要振动的窗体 { plocation form.Location; for (int i 1; i < 41; i)//41&#xff0c;可以理解为震动的时间。…

算法题复习(栈与队列、二叉树)

目录栈与队列栈用于匹配的问题队列用于堆二叉树系列深度遍历&#xff0c;递归与迭代层序遍历二叉树属性二叉树修改与构造二叉搜索树公共祖先二叉搜索树的修改与构造栈与队列 栈用于匹配的问题 20. 有效的括号 https://leetcode-cn.com/problems/valid-parentheses/ 不匹配的三…

bpsk_BPSK的完整形式是什么?

bpskBPSK&#xff1a;二进制相移键控 (BPSK: Binary Phase Shift Keying) BPSK is an abbreviation of "Binary Phase Shift Keying". BPSK是“二进制相移键控”的缩写 。 BPSK is also occasionally called phase reversal keying (PRK), or 2PSK, which is the el…

win7 下安装oracle 10g

oracle 10g 在win7下安装&#xff0c;提示程序异常终止&#xff0c;发生未知错误 在网上搜结果&#xff1a; 修改Oracle 10G\database\stage\prereq\db\refhost.xml 在 </SYSTEM> <CERTIFIED_SYSTEMS>后面添加 <!--Microsoft Windows 7--> <OPERAT…

poj 1703 Find them, Catch them

题目链接&#xff1a;http://poj.org/problem?id1703 题目大意&#xff1a;警察抓获N个罪犯&#xff0c;这些罪犯只可能属于两个团伙中的一个&#xff0c;现在给出M个条件&#xff08;D a b表示a和b不在同一团伙&#xff09;&#xff0c;对于每一个询问(A a b)确定a&#xff0…

双向a*搜索算法_双向搜索算法

双向a*搜索算法什么是双音搜索&#xff1f; (What is bitonic search?) Searching a bitonic array is known as bitonic search. An array is said to be bitonic if it has an increasing sequence of integers followed immediately by a decreasing sequence of integers.…

关于LRU缓存简单记录以及代码补全。

目录大概思路时间空间复杂度分析指针操作具体细节代码双向链表设计私有成员变量设计:构造函数和析构函数设计&#xff1a;get与put具体设计双向指针的具体细节添加到头节点函数删除尾节点函数删除节点函数删除节点函数感想今天面试考到LRU&#xff0c;太紧张了&#xff0c;完全…

码农干货系列【4】--图像识别之矩形区域搜索

简介 定位某个图片的矩形区域是非常有用的&#xff0c;这个可以通过手动的选择某个区域来实现定位&#xff0c;图片相关的软件都提供了这个功能&#xff1b;也可以像本篇一个通过程序来实现智能定位。前者会有误差&#xff0c;效率低下&#xff1b;后者选区精度高&#xff0c;效…

算法题复习(回溯)

目录base code棋盘问题51. N 皇后37. 解数独组合问题77. 组合未剪枝优化剪枝优化216. 组合总和 III未剪枝优化剪枝优化17. 电话号码的字母组合39. 组合总和未剪枝优化剪枝优化40. 组合总和 II,挺重要的&#xff0c;涉及到去重了切割问题131. 分割回文串子集问题78. 子集90. 子集…

pfa是什么意思_PFA的完整形式是什么?

pfa是什么意思PFA&#xff1a;预测性故障分析 (PFA: Predictive Failure Analysis) PFA is an abbreviation of Predictive Failure Analysis. It is a technique of a mechanism of the computer that is used to predict impending failures of software or hardware compone…

SqlServer视图(view)

--上节回顾--1.什么是事务--2.事务的特征--原子性、一致性、隔离性、持久性--3.与事务相关的t-sql命令--开启事务&#xff1a;begin transaction--提交事务&#xff1a;commit transaction--回滚事务&#xff1a;rollback transaction ----------------视图-------------------…

Android中的广播Broadcast详解

今天来看一下Android中的广播机制&#xff0c;我们知道广播Broadcast是Android中的四大组件之一&#xff0c;可见他的重要性了&#xff0c;当然它的用途也很大的&#xff0c;比如一些系统的广播&#xff1a;电量低、开机、锁屏等一些操作都会发送一个广播&#xff0c;具体的And…

sql check约束_在SQL中使用CHECK约束

sql check约束Basically, CHECK constraint is used to LIMIT in columns for the range of values. We can use this constraint for single or multiple columns. 基本上&#xff0c; CHECK约束用于限制值范围内的列 。 我们可以将此约束用于单列或多列。 In single column,…