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

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

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值