Dynamic 长度为k的递增字符串数

Dynamic 长度为k的递增字符串数,dynamic,Dynamic,我发现很难找到n个数中长度为k的递增序列的个数。我知道它有使用LIS的问题,我必须以某种方式修改它,但现在得到如何。它的复杂性是O(k*n^2)DP解决方案。请给我一点提示和解释 可能有点晚,但这可能会有所帮助: 有两种算法(据我所知)用来解决这个问题。我将用O(n^2*k)复杂度来描述这个 该算法使用DP解;这是LIS问题的一个转折点。在LIS问题中,使用1D数组存储LIS到元素i的长度,这意味着dp[i]=LIS到元素i的长度。这将产生如下算法: /* let dp[] be the arr

我发现很难找到n个数中长度为k的递增序列的个数。我知道它有使用LIS的问题,我必须以某种方式修改它,但现在得到如何。它的复杂性是
O(k*n^2)
DP解决方案。请给我一点提示和解释

可能有点晚,但这可能会有所帮助:

有两种算法(据我所知)用来解决这个问题。我将用O(n^2*k)复杂度来描述这个

该算法使用DP解;这是LIS问题的一个转折点。在LIS问题中,使用1D数组存储LIS到元素i的长度,这意味着dp[i]=LIS到元素i的长度。这将产生如下算法:

/* let dp[] be the array that stores the length of the LIS found so far.
 v[] is the array of the values

 LIS(0) = 1
 LIS(i) = 1 + max(LIS(j) where j in [1..i-1] and v[j] < v[i]);
*/

dp[0] = 1;
for (int i=1 ; i<n ; i++)
    for (int j=0 ; j<i ; j++) if (v[j] < v[i])
        dp[i] = 1 + max(dp[i], dp[j]);
/*让dp[]成为存储到目前为止找到的LIS长度的数组。
v[]是值的数组
LIS(0)=1
LIS(i)=1+max(LIS(j),其中j在[1..i-1]和v[j]数组的大小
K->我们要找的是多少
其思想是通过序列的数量来增加这个数量的值
在相同字符和前一个长度中找到。这将更好地说明int
算法
*/
总积分=0;
对于(int i=0;i
/*
  dp[i][k] -> Stores the number of subsequences of length k until character i
  v[] -> The values
  n -> The size of the array
  K -> the number of IS we are looking for

  The idea is to increment the value of this quantity, by the amount of the sequences
  found in the same character and the previous length. This will be better ilustrated int 
  the algorithm
*/

int totalIS = 0;
for (int i=0 ; i<n ; i++) {
    dp[i][1] = 1; // Number of subsequences of length 1 until element i
    for (int k=2 ; k<=K ; k++) { // For all the possible sizes of subsequence until k
        for (int j=0 ; j<i ; j++) if (v[j] < v[i]) {
            dp[i][k] += dp[j][k-1]; // Increment the actual amount by dp[j][k-1], which means
                                // the amound of IS of length k-1 until char j
        }
    }
    totalIS += dp[i][K]; // This can also be done in a separated cycle.
}

// The total amount of IS of length K is in totalIS