我读过有关的文章 如何使用动态编程确定最长的增加子序列 用这个算法:
int maxLength = 1, bestEnd = 0;
DP[0] = 1;
prev[0] = -1;
for (int i = 1; i < N; i++)
{
DP[i] = 1;
prev[i] = -1;
for (int j = i - 1; j >= 0; j--)
if (DP[j] + 1 > DP[i] && array[j] < array[i])
{
DP[i] = DP[j] + 1;
prev[i] = j;
}
if (DP[i] > maxLength)
{
bestEnd = i;
maxLength = DP[i];
}
}
但我想知道如何解决这个问题,我们可以采用连接整数的数组。
For example: 1,5,3,1,5,6,7,8,1,2,9
we can have this set:1,3,5,6,7,8,12 for solution
that 12 is joint form 1 and 2
所以条件是: 输入数组包含1-9个数字!整数可以从其他几个整数中加入。
答案 0 :(得分:2)
dp[i] = max(DP[j] + 1, a[j] < a[i])
让:
a[x,y] = a[x] + a[x + 1] + ... + a[y] (+ means concatenate)
所以:
f[x,y] = max(DP[j] + 1, a[j] < a[x,y], j < x)
dp[i] = max(f[i,j], 0 <= j <= i) = max(
max(DP[j] + 1, a[j] < a[i], j < i) # f(i, i)
max(DP[j] + 1, a[j] < a[i-1, i], j < i - 1) # f(i-1, i)
...
)
如果您仍有问题,请随时在此发表评论。