先决条件:LCS,LIS
给定两个数组,找到最长的公共递增子序列[LCIS]的长度并打印其中一个序列(可能存在多个序列)
假设我们考虑两个数组–
arr1 [] = {3,4,9,1}和
arr2 [] = {5,3,8,9,10,2,1}
我们的答案将是{3,9},因为这是最长的公共子序列,并且还在增加。
这个想法也是在这里使用动态编程。我们存储以arr2 []的每个索引结尾的最长的公共递增子序列。我们创建一个辅助数组table [],使得table [j]存储以arr2 [j]结尾的LCIS的长度。最后,我们从该表中返回最大值。为了填充该表中的值,我们遍历了arr1 []的所有元素,对于每个元素arr1 [i],我们遍历了arr2 []的所有元素。如果找到匹配项,则使用当前LCIS的长度更新表[j]。为了保持当前的LCIS,我们将继续检查有效的table [j]值。
下面是查找LCIS长度的程序。
C++
// A C++ Program to find length of the Longest Common
// Increasing Subsequence (LCIS)
#include
using namespace std;
// Returns the length and the LCIS of two
// arrays arr1[0..n-1] and arr2[0..m-1]
int LCIS(int arr1[], int n, int arr2[], int m)
{
// table[j] is going to store length of LCIS
// ending with arr2[j]. We initialize it as 0,
int table[m];
for (int j=0; j table[j])
table[j] = current + 1;
/* Now seek for previous smaller common
element for current element of arr1 */
if (arr1[i] > arr2[j])
if (table[j] > current)
current = table[j];
}
}
// The maximum value in table[] is out result
int result = 0;
for (int i=0; i result)
result = table[i];
return result;
}
/* Driver program to test above function */
int main()
{
int arr1[] = {3, 4, 9, 1};
int arr2[] = {5, 3, 8, 9, 10, 2, 1};
int n = sizeof(arr1)/sizeof(arr1[0]);
int m = sizeof(arr2)/sizeof(arr2[0]);
cout << "Length of LCIS is "
<< LCIS(arr1, n, arr2, m);
return (0);
}
Java
// A Java Program to find length of the Longest
// Common Increasing Subsequence (LCIS)
import java.io.*;
class GFG {
// Returns the length and the LCIS of two
// arrays arr1[0..n-1] and arr2[0..m-1]
static int LCIS(int arr1[], int n, int arr2[],
int m)
{
// table[j] is going to store length of
// LCIS ending with arr2[j]. We initialize
// it as 0,
int table[] = new int[m];
for (int j = 0; j < m; j++)
table[j] = 0;
// Traverse all elements of arr1[]
for (int i = 0; i < n; i++)
{
// Initialize current length of LCIS
int current = 0;
// For each element of arr1[], trvarse
// all elements of arr2[].
for (int j = 0; j < m; j++)
{
// If both the array have same
// elements. Note that we don't
// break the loop here.
if (arr1[i] == arr2[j])
if (current + 1 > table[j])
table[j] = current + 1;
/* Now seek for previous smaller
common element for current
element of arr1 */
if (arr1[i] > arr2[j])
if (table[j] > current)
current = table[j];
}
}
// The maximum value in table[] is out
// result
int result = 0;
for (int i=0; i result)
result = table[i];
return result;
}
/* Driver program to test above function */
public static void main(String[] args)
{
int arr1[] = {3, 4, 9, 1};
int arr2[] = {5, 3, 8, 9, 10, 2, 1};
int n = arr1.length;
int m = arr2.length;
System.out.println("Length of LCIS is " +
LCIS(arr1, n, arr2, m));
}
}
// This code is contributed by Prerna Saini
Python 3
# Python 3 Program to find length of the
# Longest Common Increasing Subsequence (LCIS)
# Returns the length and the LCIS of two
# arrays arr1[0..n-1] and arr2[0..m-1]
def LCIS(arr1, n, arr2, m):
# table[j] is going to store length of LCIS
# ending with arr2[j]. We initialize it as 0,
table = [0] * m
for j in range(m):
table[j] = 0
# Traverse all elements of arr1[]
for i in range(n):
# Initialize current length of LCIS
current = 0
# For each element of arr1[],
# traverse all elements of arr2[].
for j in range(m):
# If both the array have same elements.
# Note that we don't break the loop here.
if (arr1[i] == arr2[j]):
if (current + 1 > table[j]):
table[j] = current + 1
# Now seek for previous smaller common
# element for current element of arr1
if (arr1[i] > arr2[j]):
if (table[j] > current):
current = table[j]
# The maximum value in table[]
# is out result
result = 0
for i in range(m):
if (table[i] > result):
result = table[i]
return result
# Driver Code
if __name__ == "__main__":
arr1 = [3, 4, 9, 1]
arr2 = [5, 3, 8, 9, 10, 2, 1]
n = len(arr1)
m = len(arr2)
print("Length of LCIS is",
LCIS(arr1, n, arr2, m))
# This code is contributed by ita_c
C#
// A C# Program to find length of the Longest
// Common Increasing Subsequence (LCIS)
using System;
class GFG {
// Returns the length and the LCIS of two
// arrays arr1[0..n-1] and arr2[0..m-1]
static int LCIS(int []arr1, int n,
int []arr2, int m)
{
// table[j] is going to store length of
// LCIS ending with arr2[j]. We initialize
// it as 0,
int []table = new int[m];
for (int j = 0; j < m; j++)
table[j] = 0;
// Traverse all elements of arr1[]
for (int i = 0; i < n; i++)
{
// Initialize current length of LCIS
int current = 0;
// For each element of arr1[], trvarse
// all elements of arr2[].
for (int j = 0; j < m; j++)
{
// If both the array have same
// elements. Note that we don't
// break the loop here.
if (arr1[i] == arr2[j])
if (current + 1 > table[j])
table[j] = current + 1;
/* Now seek for previous smaller
common element for current
element of arr1 */
if (arr1[i] > arr2[j])
if (table[j] > current)
current = table[j];
}
}
// The maximum value in
// table[] is out result
int result = 0;
for (int i = 0; i < m; i++)
if (table[i] > result)
result = table[i];
return result;
}
/* Driver program to test above function */
public static void Main()
{
int []arr1 = {3, 4, 9, 1};
int []arr2 = {5, 3, 8, 9, 10, 2, 1};
int n = arr1.Length;
int m = arr2.Length;
Console.Write("Length of LCIS is " +
LCIS(arr1, n, arr2, m));
}
}
// This code is contributed by nitin mittal.
PHP
$table[$j])
$table[$j] = $current + 1;
/* Now seek for previous smaller
common element for current
element of arr1 */
if ($arr1[$i] > $arr2[$j])
if ($table[$j] > $current)
$current = $table[$j];
}
}
// The maximum value in
// table[] is out result
$result = 0;
for ($i = 0; $i < $m; $i++)
if ($table[$i] > $result)
$result = $table[$i];
return $result;
}
// Driver Code
$arr1 = array (3, 4, 9, 1);
$arr2 = array (5, 3, 8, 9, 10, 2, 1);
$n = sizeof($arr1);
$m = sizeof($arr2);
echo "Length of LCIS is ",
LCIS($arr1, $n, $arr2, $m);
// This code is contributed by ajit
?>
Javascript
输出 :
Length of LCIS is 2