Topic of dynamic Programming (i) HDU1087 longest common sub-sequence

Source: Internet
Author: User

Super jumping! jumping! jumping!

First of all, to find out the sub-problem of the dynamic programming problem, if the sub-problem is the longest ascending subsequence of the first n sequence, but this sub-problem is not good, because it does not have no effect, because its number of n+1 will affect the length of the first n series, in other words, if the section n+ 1 number plus go not necessarily make and the first n number together is the eldest son sequence, specific examples many such as 5,6,1,2 5th number is 3, then the longest sequence 5,6 plus 3 will not be more than 3 long.

The better sub-problem is "the first K to the end of the longest ascending subsequence ", in fact, the advantage of this sub-problem is that it limits a point, the end is the number of K, then I go to the recursion When the oldest of the 1 is in sequence, it is only related to the oldest sequence at the end of a certain point.

Next, we write its state transition equation.

MaxLen (k) indicates the length of the longest ascending subsequence for the AK as the end point

Initial state: MaxLen (1) =1

MaxLen (k) =max{MaxLen (i): 1<=i<k and AI < AK and k>=2}+1

MaxLen (k) =1 if not found

Because the sequence with less than AK as the end point, if the above conditions are met, plus AK, will certainly form a longer ascending subsequence.

In addition, from the understanding point of view, AK is from the end of the 1 to k-1 each judge down , then once they are ascending, the associated will be the AI's eldest son sequence length to go up, make it become longer.

The subject only made a small change the longest rise sum, the idea is roughly the same, the code is as follows

1#include <iostream>2#include <cstdio>3#include <cstring>4 #defineMAXN 10055 using namespacestd;6 intNUM[MAXN],M[MAXN];//m Records the longest rising sum of each end point7 intMain ()8 {9     intt,i,j,k;Ten      while(cin>>t) One     { A         if(t==0) -              Break; -Memset (M,0,sizeof(0)); the          for(i=1; i<=t;i++) -scanf"%d",&num[i]); -m[1]=num[1]; -          for(i=2; i<=t;i++) +         { -m[i]=Num[i]; +              for(j=1; j<i;j++) A             { at                 if(num[j]<Num[i]) -M[i]=max (m[i],m[j]+num[i]); -             } -         } -k=m[1]; -          for(i=1; i<=t;i++) inK=m[i]>k?m[i]:k; -cout<<k<<Endl; to     } +     return 0; -}

Topic of dynamic Programming (i) HDU1087 longest common sub-sequence

Related Article

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.