Dynamic Planning III-serialization Dynamic Planning

Source: Internet
Author: User
In section 3, we will discuss the dynamic planning solution in serialization. This part is divided into single sequence and double sequence.

Example 1: longest ascending subsequence.

For the longest ascending subsequence problem, there is a positive integer series. The length of N is within 1000, and element a [I] is within 10 ^ 5, and the length of the longest ascending subsequence is obtained.


Analysis 1: differentiated nature of the problem
If we use the exhaustive method, there will be 2 ^ n of time complexity; there are many repeated 4, 3, and ** type subsequences, the length of the incremental subsequence starting with 4 is 1.
Obviously, I can write recursive functions.
DP (int I) = {
A [I] = 1;
For (Int J = I + 1; j <n; ++ J ){
If (A [I] <A [J])
Temp = max (A [I], dp (j ));
}
Return temp;
}: The longest incremental length of the subsequence starting with a [I], and then select the largest from DP (I.


Here, the time complexity is O (n ^ 2). After recursion and divide and conquer, did we find the problem? Here, we can easily turn it into a dynamic planning problem. There are two forms. You can implement them by yourself: 1) DP [I]: the length of the longest incrementing subsequence starting with a [I]; 2) the length of the longest incrementing sub-sequence ending with a [I] by DP [I]


Analysis 2: Search Optimization
Suppose DP [I]: The maximum Lis length ending with a [I. Obviously, we increase DP [I] in the ascending order of I. In this way, if DP [J] (j <I) has the same length, we need to select the smallest one of a [J] for calculation, instead of traversing. Therefore, we define the minimum value (INF) of the ending element in the subsequence with the length of DP [I] Being I + 1 ).
For each a [J], if I = 0 or DP [I-1] <A [J], DP [I] = min (DP [I], a [J])
For (INT I = 0; I <n; ++ I ){
For (j = 0; j <n; ++ J ){
If (I = 0 | DP [I-1] <A [J]) DP [I] = min (DP [I], a [J]);
}
}
In this case, there are two loops: length L and array subscript J; time complexity is still O (N ^ 2); however, there is an improvement: because the array DP [I] is monotonically incrementing, for each a [J], only one update is required at most (the elements ending with Lis cannot be a [J]). therefore, we can use binary search to determine the updated DP [I];
For (I = 0; I <n; ++ I ){
* Lower_bound (DP, DP + n, a [I]) = A [I];
}
Return lower_bound (DP, DP + N, INF)-DP;

Eg2: Word breakgiven a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words.


For example, given
S = "leetcode ",
Dict = ["Leet", "code"].


Return true because "leetcode" can be segmented as "Leet code ".
Analysis:
Obviously, we can first divide the problem into two parts: Find the string S1 starting with s [0] In dict, and then the rest is S2; then we can determine whether solution (S2) is satisfied.
After careful analysis, we can find that adding dict = ["AB", "Abab"], S = "ababcd". In this case, we need to determine solution ("ABCD "), solution ("cd"), while solution ("cd") is included in solution ("ABCD. Therefore, the problem satisfies the overlapping subproblem and the optimal sub-structure. The time complexity is O (n ^ 2 ).

class Solution {                public:                    bool wordBreak(string s, unordered_set<string> &dict) {             int n=s.size();             if(n==0) return true;             bool dp[n+1];             fill(dp, dp+n+1, false);             dp[0]=true;             int i, j;                                  for (i = 0; i < n; ++i){                 for(j=i;j>=0;j--){                    if(dp[j]){                        if( dict.find(s.substr(j, i+1 - j)) != dict.end() ){                            dp[i+1]=true;                            break;                        }                       }                    }                }                return dp[n];         }   };  

Further analysis: if there is such an example, S = "aaaaaaaaaaaaa... A "(100 A); similarly, the element in dict is S. In this way, the time complexity is still O (N ^ 2). In fact, DP [0] ~ DP [n-1] results are 0. when we calculate DP [I], our time complexity is also O (n), which can be reduced to O (dict. size ()). on the other hand, we use linear lookup to determine that D [J] is true. In fact, we can use a stack to store J corresponding to true for DP [J, this can also reduce the time complexity to a certain extent.

Dynamic Planning III-serialization Dynamic Planning

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.