如果一个序列先增加然后减少,则称其为双子序列。在这个问题中,给出了所有正整数的数组。我们必须找到一个先增大然后减小的子序列。
为了解决这个问题,我们将定义两个子序列,它们是最长增加子序列和最长减少子序列。LIS数组将保留以array [i]结尾的递增子序列的长度。LDS数组将存储从array [i]开始的递减子序列的长度。使用这两个数组,我们可以获得最长的双子序列的长度。
Input: A sequence of numbers. {0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15} Output: The longest bitonic subsequence length. Here it is 7.
longBitonicSub(array, size)
输入:数组,数组的大小。
输出-最长双子序列的最大长度。
Begin define incSubSeq of size same as the array size initially fill all entries to 1 for incSubSeq for i := 1 to size -1, do for j := 0 to i-1, do if array[i] > array[j] and incSubSeq[i] < incSubSum[j] + 1, then incSubSum[i] := incSubSum[j] + 1 done done define decSubSeq of size same as the array size. initially fill all entries to 1 for incSubSeq for i := size - 2 down to 0, do for j := size - 1 down to i+1, do if array[i] > array[j] and decSubSeq[i] < decSubSum[j] + 1, then decSubSeq [i] := decSubSeq [j] + 1 done done max := incSubSeq[0] + decSubSeq[0] – 1 for i := 1 to size, do if incSubSeq[i] + decSubSeq[i] – 1 > max, then max := incSubSeq[i] + decSubSeq[i] – 1 done return max End
#include<iostream> using namespace std; int longBitonicSub( int arr[], int size ) { int *increasingSubSeq = new int[size]; //create increasing sub sequence array for (int i = 0; i < size; i++) increasingSubSeq[i] = 1; //set all values to 1 for (int i = 1; i < size; i++) //compute values from left ot right for (int j = 0; j < i; j++) if (arr[i] > arr[j] && increasingSubSeq[i] < increasingSubSeq[j] + 1) increasingSubSeq[i] = increasingSubSeq[j] + 1; int *decreasingSubSeq = new int [size]; //create decreasing sub sequence array for (int i = 0; i < size; i++) decreasingSubSeq[i] = 1; //set all values to 1 for (int i = size-2; i >= 0; i--) //compute values from left ot right for (int j = size-1; j > i; j--) if (arr[i] > arr[j] && decreasingSubSeq[i] < decreasingSubSeq[j] + 1) decreasingSubSeq[i] = decreasingSubSeq[j] + 1; int max = increasingSubSeq[0] + decreasingSubSeq[0] - 1; for (int i = 1; i < size; i++) //find max length if (increasingSubSeq[i] + decreasingSubSeq[i] - 1 > max) max = increasingSubSeq[i] + decreasingSubSeq[i] - 1; return max; } int main() { int arr[] = {0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15}; int n = 16; cout << "Length of longest bitonic subsequence is " << longBitonicSub(arr, n); }
输出结果
Length of longest bitonic subsequence is 7
这里,在这段代码中,它打印序列的最大子序列的长度,该序列先增加后减少,或者反之亦然。 例如: 输入:1,11,2,10,4,5,2,1 如增-减-增或减-增-减 示例: 投入:7 16 1 6 20 17 7 18 25 1 25 21 11 5 29 11 3 3 26 19
然而,我意识到我真正想解决的问题有点不同。给定一个固定的k,我需要确保公共子序列只涉及长度正好为k的子串。例如,设置k=2,并让这两个字符串为 我需要的子序列是“。 是否可以修改动态规划解来解决这个问题?
在最多一个序列存在重复的情况下,可以将最长公共子序列问题转化为最长递增子序列问题。减少问题的过程说明在这里: 假设您有以下序列: 然后,创建一个整数序列S3,其中您必须将S2的每个元素的位置放在S1中(如果元素在S1中不存在,那么忽略那个元素)。在本例中: 这种方法是如何工作的?为什么这种约简解决了寻找最长公共子序列的问题?
我的问题是,这个问题有没有更好的解决办法?多谢.
我目前正在尝试为2个给定字符串查找和打印最长的公共子序列。我使用最常见的算法,没有递归。如果我保留整个数组,这是一项简单的任务,但我正在尝试对其进行一点优化,只使用2行,您可以在下面的代码中看到。有了这个更改,查找长度仍然很简单,工作正常,但恢复子序列不再那么容易了。我尝试了几种方法,但都不起作用。下面你可以看到我最后的尝试。虽然它适用于相同的情况,但也有失败的情况。经过长时间的思考,我开始相信没
我认为这个问题可以用动态规划来解决,但我不能提出递归关系。
、和是子序列中的三个连续元素。 例如,如果输入数组为,则最长凸子序列应为:或。 在“最长递增子序列”(LIS)问题中,我尝试用同样的动态规划思想来解决这个问题。但是由于子序列中的每个元素都依赖于前面的两个元素,所以O(n^2)解似乎是不可能的。谢谢你的帮助。
问题内容: python中是否有内置函数返回两个列表的最长公共子序列的长度? 我试图找到最长的公共子序列,然后得到它的长度,但是我认为必须有一个更好的解决方案。 问题答案: 您可以轻松地将LCS重新装配为LLCS: 演示: 如果您想要最长的公共 子字符串 (一个 不同 但相关的问题, 子 序列是连续的),请使用: 这与动态编程方法非常相似,但是我们跟踪到目前为止找到的最大长度(因为不再保证表中的最