Problem
Given two sequences A and B, the subsequence of a sequence is a subset of the number of numbers formed from the original sequence in the order of gradual increment of the index, and if the numerical size of the subsequence is gradually ascending to the ascending sub-sequence, if the two subsequence A1 and B1 are the same, then the common subsequence of A and B is a1/b1. Find the longest common ascending subsequence of A and B.
Analysis
In conjunction with the longest common subsequence and the longest ascending subsequence to solve this problem, the definition state Dp[i][j] represents the length of the longest public ascending subsequence of the first I character in a string and the first J character in string B and ends with B[j] . Then there is the state transfer equation: "In the design of dynamic planning state, we should describe the state information in a simple and detailed"
if (a[i]! = b[j]) dp[i][j] = Dp[i-1][j];else dp[i][j] = max (dp[i-1][k]) + 1; (1 <= k <= j-1 && b[j] > B[k])
Realize
for (int i = 1; I <= n; i + +) { int maxn = 0; for (int j = 1; j <= M; j + +) { if (A[i]!=b[j]) dp[i][j] = dp[i-1][j]; if (A[i] > B[j]) MAXN = max (MAXN, Dp[i-1][j]); if (a[i] = = B[j]) dp[i][j] = maxn + 1; }} int mm = 0;for (int i = 1;i <= m; i + +) mm = max (mm, dp[n][i]);
Dynamic programming--the longest common ascending subsequence LCIs