📌  相关文章
📜  给定两个数组中最长的公共子数组

📅  最后修改于: 2021-04-27 20:11:32             🧑  作者: Mango

给定分别为N和M个整数的两个数组A []和B [] ,任务是找到两个给定数组之间相等的子数组的最大长度或最长的公共子数组。

例子:

天真的方法:想法是生成两个给定数组A []B []的所有子数组,并找到最长的匹配子数组。就时间复杂度而言,此解决方案是指数级的。
时间复杂度: O(2 N + M ),其中N是数组A []的长度,M是数组B []的长度。

高效方法:
有效的方法是使用动态编程(DP)。此问题是最长公共子序列(LCS)的变体。
令输入序列分别为长度为m和n的A [0..n-1]B [0..m-1] 。以下是equal子数组的递归实现:

  1. 由于A []B []的公共子数组必须从某个索引ij开始,使得A [i]等于B [j] 。令dp [i] [j]A [i …]和B [j …]的最长公共子数组。
  2. 因此,对于任何索引i和j,如果A [i]等于B [j],则dp [i] [j] = dp [i + 1] [j + 1] +1
  3. 数组dp [] []中所有元素的最大值将给出相等子数组的最大长度。

例如:
如果给定数组A [] = {1,2,8,2,1}和B [] = {8,2,1,4,7}。如果字符在数组A []B []的索引ij处分别匹配,则dp [i] [j]将更新为1 + dp [i + 1] [j + 1]
以下是给定数组A []B []的更新后的dp [] []表。

下面是上述方法的实现:

C++
// C++ program to DP approach
// to above solution
#include 
using namespace std;
 
// Function to find the maximum
// length of equal subarray
int FindMaxLength(int A[], int B[], int n, int m)
{
 
    // Auxillary dp[][] array
    int dp[n + 1][m + 1];
    for (int i = 0; i <= n; i++)
        for (int j = 0; j <= m; j++)
            dp[i][j] = 0;
 
    // Updating the dp[][] table
    // in Bottom Up approach
    for (int i = n - 1; i >= 0; i--)
    {
        for (int j = m - 1; j >= 0; j--)
        {
            // If A[i] is equal to B[i]
            // then dp[j][i] = dp[j + 1][i + 1] + 1
            if (A[i] == B[j])
                dp[j][i] = dp[j + 1][i + 1] + 1;
        }
    }
    int maxm = 0;
 
    // Find maximum of all the values
    // in dp[][] array to get the
    // maximum length
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            // Update the length
            maxm = max(maxm, dp[i][j]);
        }
    }
 
    // Return the maximum length
    return maxm;
}
 
// Driver Code
int main()
{
    int A[] = { 1, 2, 8, 2, 1 };
    int B[] = { 8, 2, 1, 4, 7 };
 
    int n = sizeof(A) / sizeof(A[0]);
    int m = sizeof(B) / sizeof(B[0]);
 
    // Function call to find
    // maximum length of subarray
    cout << (FindMaxLength(A, B, n, m));
}
 
// This code is contributed by chitranayal


Java
// Java program to DP approach
// to above solution
class GFG
{
    // Function to find the maximum
    // length of equal subarray
    static int FindMaxLength(int A[], int B[], int n, int m)
    {
 
        // Auxillary dp[][] array
        int[][] dp = new int[n + 1][m + 1];
        for (int i = 0; i <= n; i++)
            for (int j = 0; j <= m; j++)
                dp[i][j] = 0;
 
        // Updating the dp[][] table
        // in Bottom Up approach
        for (int i = n - 1; i >= 0; i--)
        {
            for (int j = m - 1; j >= 0; j--)
            {
                // If A[i] is equal to B[i]
                // then dp[j][i] = dp[j + 1][i + 1] + 1
                if (A[i] == B[j])
                    dp[j][i] = dp[j + 1][i + 1] + 1;
            }
        }
        int maxm = 0;
 
        // Find maximum of all the values
        // in dp[][] array to get the
        // maximum length
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                // Update the length
                maxm = Math.max(maxm, dp[i][j]);
            }
        }
 
        // Return the maximum length
        return maxm;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int A[] = { 1, 2, 8, 2, 1 };
        int B[] = { 8, 2, 1, 4, 7 };
 
        int n = A.length;
        int m = B.length;
 
        // Function call to find
        // maximum length of subarray
        System.out.print(FindMaxLength(A, B, n, m));
    }
}
 
// This code is contributed by PrinciRaj1992


Python
# Python program to DP approach
# to above solution
 
# Function to find the maximum
# length of equal subarray
 
 
def FindMaxLength(A, B):
    n = len(A)
    m = len(B)
 
    # Auxillary dp[][] array
    dp = [[0 for i in range(n + 1)] for i in range(m + 1)]
 
    # Updating the dp[][] table
    # in Bottom Up approach
    for i in range(n - 1, -1, -1):
        for j in range(m - 1, -1, -1):
 
            # If A[i] is equal to B[i]
            # then dp[j][i] = dp[j + 1][i + 1] + 1
            if A[i] == B[j]:
                dp[j][i] = dp[j + 1][i + 1] + 1
    maxm = 0
 
    # Find maximum of all the values
    # in dp[][] array to get the
    # maximum length
    for i in dp:
        for j in i:
 
            # Update the length
            maxm = max(maxm, j)
 
    # Return the maximum length
    return maxm
 
 
# Driver Code
if __name__ == '__main__':
    A = [1, 2, 8, 2, 1]
    B = [8, 2, 1, 4, 7]
 
    # Function call to find
    # maximum length of subarray
    print(FindMaxLength(A, B))


C#
// C# program to DP approach
// to above solution
using System;
 
class GFG
{
    // Function to find the maximum
    // length of equal subarray
    static int FindMaxLength(int[] A, int[] B, int n, int m)
    {
 
        // Auxillary [,]dp array
        int[, ] dp = new int[n + 1, m + 1];
        for (int i = 0; i <= n; i++)
            for (int j = 0; j <= m; j++)
                dp[i, j] = 0;
 
        // Updating the [,]dp table
        // in Bottom Up approach
        for (int i = n - 1; i >= 0; i--)
        {
            for (int j = m - 1; j >= 0; j--)
            {
                // If A[i] is equal to B[i]
                // then dp[j, i] = dp[j + 1, i + 1] + 1
                if (A[i] == B[j])
                    dp[j, i] = dp[j + 1, i + 1] + 1;
            }
        }
        int maxm = 0;
 
        // Find maximum of all the values
        // in [,]dp array to get the
        // maximum length
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
 
                // Update the length
                maxm = Math.Max(maxm, dp[i, j]);
            }
        }
 
        // Return the maximum length
        return maxm;
    }
 
    // Driver Code
    public static void Main(String[] args)
    {
        int[] A = { 1, 2, 8, 2, 1 };
        int[] B = { 8, 2, 1, 4, 7 };
 
        int n = A.Length;
        int m = B.Length;
 
        // Function call to find
        // maximum length of subarray
        Console.Write(FindMaxLength(A, B, n, m));
    }
}
 
// This code is contributed by PrinciRaj1992


输出
3

时间复杂度: O(N * M),其中N是数组A []的长度,M是数组B []的长度。