CH5101 LCIs "Linear DP"

Source: Internet
Author: User
Tags cmath

5101 LCIs 0x50"Dynamic Programming" examplesDescribe

Mother Bear's cows under the influence of the small Mu Mu began to study information topics. Small mu mu first let the cows study the longest ascending subsequence, and then let them study the longest common sub-sequence, and now let them study the longest public ascending sub-sequence.
Small Mu Mu said, for two series A and B, if they all contain a position is not necessarily continuous number, and the value is strictly increment, then said that the number is two series of public ascending sub-sequence, and all of the public ascending sub-sequence of the longest is the longest public ascending sub-sequence.
The cow half understand, small mu mu want you to tell the cow what is the longest public ascending sub-sequence. But just tell the cow how long it's going to be. The lengths of Series A and B are not more than 3000.

Input format

The first row n, which represents the length of a/b.
Second line, string A.
Third row, String B.

Output format

The output length.

Sample input
42 2 1 32 1 2 3
Sample output
2
Data scope and conventions
    • The number in 1<=n<=3000,a,b does not exceed 2^31-1

Contest Hunter-Self-help competition platform for Informatics

The combination of LIS and LCS problems

DP[I][J] Indicates the length of LCIs in A1-ai and B1-BJ with BJ as the end

So when AI! = Bj when obviously dp[i][j] = Dp[i-1][j]

When ai = = BJ To determine whether there is BK < BJ in the 1-j to take all possible solutions to the maximum value plus one

When you look for K, you can run for a loop, so the total complexity is n^3.

The optimized algorithm is for each i a[i] without changing the number of possible solutions for each J increment is incremented

Each time you record the maximum possible solution for the current J is available for the next update

"When implementing state transition equations, it is important to observe the scope of the decision set as the state changes." For a scenario where the elements in a decision collection are only increasing in number, you can maintain a variable as a subject to record the current information for the decision collection, avoiding duplicate scans "

The third set of data seems to be a problem or something. Anyway, I got one more out of the output, WA, and I just got rid of it.

N^2 algorithm

1 //#include <bits/stdc++.h>2#include <iostream>3#include <cmath>4#include <algorithm>5#include <stdio.h>6#include <cstring>7 8 using namespacestd;9typedefLong Long intLL;Ten  One Const intMAXN =3005; A intA[MAXN], B[MAXN]; - intN; - intDP[MAXN][MAXN]; the  - intMain () - { -scanf"%d", &n); +          for(inti =1; I <= N; i++) -scanf"%d", &a[i]); +          for(inti =1; I <= N; i++) Ascanf"%d", &b[i]); at  -         //memset (DP, 0, sizeof (DP)); -          for(inti =1; I <= N; i++){ -             intval =0; -              for(intj =1; J <= N; J + +){ -                 if(A[i] = =B[j]) { inDP[I][J] = val +1; -                 } to                 Else{ +DP[I][J] = dp[i-1][j]; -                 } the                 if(B[j] < a[i]) val = max (val, Dp[i-1][j]); *             } $         }Panax Notoginseng  -         intAns =0; the          for(inti =1; I <= N; i++){ +Ans =Max (ans, dp[n][i]); A         } the         //cout<<endl; +printf"%d", ans); -         //scanf ("%d", &n); $     return 0; $}

N^3 algorithm

1 //#include <bits/stdc++.h>2#include <iostream>3#include <cmath>4#include <algorithm>5#include <stdio.h>6#include <cstring>7 8 using namespacestd;9typedefLong Long intLL;Ten  One Const intMAXN =3005; A intA[MAXN], B[MAXN]; - intN; - intDP[MAXN][MAXN]; the  - intMain () - { -scanf"%d", &n); +          for(inti =1; I <= N; i++) -scanf"%d", &a[i]); +          for(inti =1; I <= N; i++) Ascanf"%d", &b[i]); at  -         //memset (DP, 0, sizeof (DP)); -          for(inti =1; I <= N; i++){ -              for(intj =1; J <= N; J + +){ -                 if(A[i] = =B[j]) { -                      for(intK =0; K < J; k++){ in                         if(B[k] <A[i]) -DP[I][J] = max (Dp[i-1][K] +1, Dp[i][j]); to                     } +                 } -                 Else{ theDP[I][J] = dp[i-1][j]; *                 } $             }Panax Notoginseng         } -  the         intAns =0; +          for(inti =1; I <= N; i++){ AAns =Max (ans, dp[n][i]); the         } +         //cout<<endl; -printf"%d", ans); $         //scanf ("%d", &n); $     return 0; -}
View Code

CH5101 LCIs "Linear DP"

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.