📜  一个数组的最长子数组,它是另一个数组中的子序列

📅  最后修改于: 2021-09-03 03:37:26             🧑  作者: Mango

给定两个数组arr1[]arr2[] ,任务是找到arr1[]的最长子数组,它是arr2[] 的子序列。

例子:

方法:思路是用动态规划来解决这个问题。请按照以下步骤解决问题:

  • 初始化一个DP[][]表,其中DP[i][j]存储最长子数组的长度,直到arr1[] 中的i索引,它是arr2[] 中直到第j索引的子序列。
  • 现在,遍历两个数组并执行以下操作:
    • 情况 1:如果arr1[i]arr2[j]相等,则将DP[i – 1][j – 1]1 ,因为arr1[i]arr2[j]对所需的最长子数组长度有贡献。
    • 情况 2:如果arr1[i]arr2[j]不相等,则设置DP[i][j] = DP[i – 1][j]
  • 最后,打印DP[][]表中存在的最大值作为所需答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
    // Function to find the length of the
    // longest subarray in arr1[] which
    // is a subsequence in arr2[]
    int LongSubarrSeq(int arr1[], int arr2[], int M, int N)
    {
        // Length of the array arr1[]
 
        // Length of the required
        // longest subarray
        int maxL = 0;
 
        // Initialize DP[]array
        int DP[M + 1][N + 1];
 
        // Traverse array arr1[]
        for (int i = 1; i <= M; i++)
        {
 
            // Traverse array arr2[]
            for (int j = 1; j <= N; j++)
            {
                if (arr1[i - 1] == arr2[j - 1])
                {
 
                    // arr1[i - 1] contributes to
                    // the length of the subarray
                    DP[i][j] = 1 + DP[i - 1][j - 1];
                }
 
                // Otherwise
                else
                {
 
                    DP[i][j] = DP[i][j - 1];
                }
            }
        }
 
        // Find the maximum value
        // present in DP[][]
        for (int i = 1; i <= M; i++)
        {
            for (int j = 1; j <= N; j++)
            {
                maxL = max(maxL, DP[i][j]);
            }
        }
 
        // Return the result
        return maxL;
    }
 
 
// Driver Code
int main()
{
    int arr1[] = { 4, 2, 3, 1, 5, 6 };
    int M = sizeof(arr1) / sizeof(arr1[0]);
     
    int arr2[] = { 3, 1, 4, 6, 5, 2 };
    int N = sizeof(arr2) / sizeof(arr2[0]);
 
    // Function call to find the length
    // of the longest required subarray
    cout << LongSubarrSeq(arr1, arr2, M, N) <


Java
// Java program
// for the above approach
 
import java.io.*;
 
class GFG {
 
    // Function to find the length of the
    // longest subarray in arr1[] which
    // is a subsequence in arr2[]
    private static int LongSubarrSeq(
        int[] arr1, int[] arr2)
    {
        // Length of the array arr1[]
        int M = arr1.length;
 
        // Length of the array arr2[]
        int N = arr2.length;
 
        // Length of the required
        // longest subarray
        int maxL = 0;
 
        // Initialize DP[]array
        int[][] DP = new int[M + 1][N + 1];
 
        // Traverse array arr1[]
        for (int i = 1; i <= M; i++) {
 
            // Traverse array arr2[]
            for (int j = 1; j <= N; j++) {
 
                if (arr1[i - 1] == arr2[j - 1]) {
 
                    // arr1[i - 1] contributes to
                    // the length of the subarray
                    DP[i][j] = 1 + DP[i - 1][j - 1];
                }
 
                // Otherwise
                else {
 
                    DP[i][j] = DP[i][j - 1];
                }
            }
        }
 
        // Find the maximum value
        // present in DP[][]
        for (int i = 1; i <= M; i++) {
 
            for (int j = 1; j <= N; j++) {
 
                maxL = Math.max(maxL, DP[i][j]);
            }
        }
 
        // Return the result
        return maxL;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int[] arr1 = { 4, 2, 3, 1, 5, 6 };
        int[] arr2 = { 3, 1, 4, 6, 5, 2 };
 
        // Function call to find the length
        // of the longest required subarray
        System.out.println(LongSubarrSeq(arr1, arr2));
    }
}


Python3
# Python program
# for the above approach
 
# Function to find the length of the
# longest subarray in arr1 which
# is a subsequence in arr2
def LongSubarrSeq(arr1, arr2):
   
    # Length of the array arr1
    M = len(arr1);
 
    # Length of the array arr2
    N = len(arr2);
 
    # Length of the required
    # longest subarray
    maxL = 0;
 
    # Initialize DParray
    DP = [[0 for i in range(N + 1)] for j in range(M + 1)];
 
    # Traverse array arr1
    for i in range(1, M + 1):
 
        # Traverse array arr2
        for j in range(1, N + 1):
            if (arr1[i - 1] == arr2[j - 1]):
 
                # arr1[i - 1] contributes to
                # the length of the subarray
                DP[i][j] = 1 + DP[i - 1][j - 1];
 
            # Otherwise
            else:
 
                DP[i][j] = DP[i][j - 1];
 
    # Find the maximum value
    # present in DP
    for i in range(M + 1):
 
        # Traverse array arr2
        for j in range(1, N + 1):
            maxL = max(maxL, DP[i][j]);
 
    # Return the result
    return maxL;
 
# Driver Code
if __name__ == '__main__':
    arr1 = [4, 2, 3, 1, 5, 6];
    arr2 = [3, 1, 4, 6, 5, 2];
 
    # Function call to find the length
    # of the longest required subarray
    print(LongSubarrSeq(arr1, arr2));
 
    # This code contributed by shikhasingrajput


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to find the length of the
// longest subarray in arr1[] which
// is a subsequence in arr2[]
private static int LongSubarrSeq(int[] arr1,
                                 int[] arr2)
{
     
    // Length of the array arr1[]
    int M = arr1.Length;
     
    // Length of the array arr2[]
    int N = arr2.Length;
     
    // Length of the required
    // longest subarray
    int maxL = 0;
     
    // Initialize DP[]array
    int[,] DP = new int[M + 1, N + 1];
 
    // Traverse array arr1[]
    for(int i = 1; i <= M; i++)
    {
         
        // Traverse array arr2[]
        for(int j = 1; j <= N; j++)
        {
            if (arr1[i - 1] == arr2[j - 1])
            {
                 
                // arr1[i - 1] contributes to
                // the length of the subarray
                DP[i, j] = 1 + DP[i - 1, j - 1];
            }
 
            // Otherwise
            else
            {
                DP[i, j] = DP[i, j - 1];
            }
        }
    }
 
    // Find the maximum value
    // present in DP[][]
    for(int i = 1; i <= M; i++)
    {
        for(int j = 1; j <= N; j++)
        {
            maxL = Math.Max(maxL, DP[i, j]);
        }
    }
     
    // Return the result
    return maxL;
}
 
// Driver Code
static public void Main()
{
    int[] arr1 = { 4, 2, 3, 1, 5, 6 };
    int[] arr2 = { 3, 1, 4, 6, 5, 2 };
     
    // Function call to find the length
    // of the longest required subarray
    Console.WriteLine(LongSubarrSeq(arr1, arr2));
}
}
 
// This code is contributed by susmitakundugoaldanga


Javascript


输出:
3

时间复杂度: O(M * N)
辅助空间: O(M * N)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live