📜  使用最长公共子序列算法的最长增长子序列

📅  最后修改于: 2021-06-26 21:25:41             🧑  作者: Mango

给定N个整数的数组arr [] ,任务是查找并打印最长增加子序列。
例子:

先决条件:LCS,LIS
方法:任何序列中最长的递增子序列是其自身已排序序列的子序列。可以使用动态编程方法解决。该方法与经典LCS问题相同,但是代替了第二个序列,给定序列以其排序形式再次使用。
注意:数组应具有不同的元素,否则可能会产生错误的结果。例如,在{1,1,1}中,我们知道最长的递增子序列(a1 下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the size of the
// longest increasing subsequence
int LISusingLCS(vector& seq)
{
    int n = seq.size();
 
    // Create an 2D array of integer
    // for tabulation
    vector > L(n + 1, vector(n + 1));
 
    // Take the second sequence as the sorted
    // sequence of the given sequence
    vector sortedseq(seq);
 
    sort(sortedseq.begin(), sortedseq.end());
 
    // Classical Dynamic Programming algorithm
    // for Longest Common Subsequence
    for (int i = 0; i <= n; i++) {
        for (int j = 0; j <= n; j++) {
            if (i == 0 || j == 0)
                L[i][j] = 0;
 
            else if (seq[i - 1] == sortedseq[j - 1])
                L[i][j] = L[i - 1][j - 1] + 1;
 
            else
                L[i][j] = max(L[i - 1][j], L[i][j - 1]);
        }
    }
 
    // Return the ans
    return L[n][n];
}
 
// Driver code
int main()
{
 
    vector sequence{ 12, 34, 1, 5, 40, 80 };
 
    cout << LISusingLCS(sequence) << endl;
 
    return 0;
}


Java
//Java implementation of above approach
import java.util.*;
 
class GFG
{
     
// Function to return the size of the
// longest increasing subsequence
static int LISusingLCS(Vector seq)
{
    int n = seq.size();
 
    // Create an 2D array of integer
    // for tabulation
    int L[][] = new int [n + 1][n + 1];
 
    // Take the second sequence as the sorted
    // sequence of the given sequence
    Vector sortedseq = new Vector(seq);
 
    Collections.sort(sortedseq);
 
    // Classical Dynamic Programming algorithm
    // for Longest Common Subsequence
    for (int i = 0; i <= n; i++)
    {
        for (int j = 0; j <= n; j++)
        {
            if (i == 0 || j == 0)
                L[i][j] = 0;
 
            else if (seq.get(i - 1) == sortedseq.get(j - 1))
                L[i][j] = L[i - 1][j - 1] + 1;
 
            else
                L[i][j] = Math.max(L[i - 1][j],
                                   L[i][j - 1]);
        }
    }
 
    // Return the ans
    return L[n][n];
}
 
// Driver code
public static void main(String args[])
{
    Vector sequence = new Vector();
    sequence.add(12);
    sequence.add(34);
    sequence.add(1);
    sequence.add(5);
    sequence.add(40);
    sequence.add(80);
 
    System.out.println(LISusingLCS(sequence));
}
}
 
// This code is contributed by Arnab Kundu


Python3
# Python3 implementation of the approach
 
# Function to return the size of the
# longest increasing subsequence
def LISusingLCS(seq):
    n = len(seq)
 
    # Create an 2D array of integer
    # for tabulation
    L = [[0 for i in range(n + 1)]
            for i in range(n + 1)]
     
    # Take the second sequence as the sorted
    # sequence of the given sequence
    sortedseq = sorted(seq)
 
    # Classical Dynamic Programming algorithm
    # for Longest Common Subsequence
    for i in range(n + 1):
        for j in range(n + 1):
            if (i == 0 or j == 0):
                L[i][j] = 0
 
            elif (seq[i - 1] == sortedseq[j - 1]):
                L[i][j] = L[i - 1][j - 1] + 1
 
            else:
                L[i][j] = max(L[i - 1][j],
                              L[i][j - 1])
 
    # Return the ans
    return L[n][n]
 
# Driver code
sequence = [12, 34, 1, 5, 40, 80]
 
print(LISusingLCS(sequence))
 
# This code is contributed by mohit kumar


C#
// C# implementation of above approach
using System;
using System.Collections.Generic;
 
class GFG
{
     
// Function to return the size of the
// longest increasing subsequence
static int LISusingLCS(List seq)
{
    int n = seq.Count;
 
    // Create an 2D array of integer
    // for tabulation
    int [,]L = new int [n + 1, n + 1];
 
    // Take the second sequence as the sorted
    // sequence of the given sequence
    List sortedseq = new List(seq);
 
    sortedseq.Sort();
 
    // Classical Dynamic Programming algorithm
    // for longest Common Subsequence
    for (int i = 0; i <= n; i++)
    {
        for (int j = 0; j <= n; j++)
        {
            if (i == 0 || j == 0)
                L[i, j] = 0;
 
            else if (seq[i - 1] == sortedseq[j - 1])
                L[i, j] = L[i - 1, j - 1] + 1;
 
            else
                L[i,j] = Math.Max(L[i - 1, j],
                                L[i, j - 1]);
        }
    }
 
    // Return the ans
    return L[n, n];
}
 
// Driver code
public static void Main(String []args)
{
    List sequence = new List();
    sequence.Add(12);
    sequence.Add(34);
    sequence.Add(1);
    sequence.Add(5);
    sequence.Add(40);
    sequence.Add(80);
 
    Console.WriteLine(LISusingLCS(sequence));
}
}
 
// This code is contributed by 29AjayKumar


输出:
4

时间复杂度: O(n 2 ),其中n是序列的长度。

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。