The story of HDU-4512 jige series -- The Longest Common ascending subsequence in perfect formation

Source: Internet
Author: User

Given a sequence, find the longest symmetric sequence in the sequence, and keep the left side increasing and right side decreasing in the symmetry of the sequence.

Solution: Flip the original string and find the longest common ascending subsequence. Note the boundary: match from the I-bit of the original string, then the flip string can not match to [1, I-1] This interval, otherwise the invalid match results will be left down and right increase.

Code entry:

# Include <cstdlib> # include <cstring> # include <cstdio> # include <cstdlib> # include <iostream> # include <algorithm> using namespace STD; int N; int A [205]; int f [205] [205]; int DP [205]; /* define the State f [I] [J] to indicate the length of lcis composed of the first J characters of string B whose names start with string a and end with string B [J ]. due to the increasing restrictions, therefore, we need to consider the last value of the previous State. This is also a limit value. By setting a state that contains a specific location deadline, it is helpful to save this value */void solve () {int max =-1, Len;/* Two-Dimensional Space writing memset (F, 0, sizeof (f); For (INT I = 1; I <= N; ++ I) {Len = 0; For (Int J = N; j> = I; -- j) {if (a [I] = A [J]) {f [I] [J] = Len + 1; if (j> I) {max = max (max, F [I] [J] * 2 );} else {max = max (max, F [I] [J] * 2-1 );}} else {f [I] [J] = f [I-1] [J]; if (a [I]> A [J]) {// Len maintain the maximum currently available length of a [I] Len = max (Len, F [I-1] [J]);} * // write memset (DP, 0, sizeof (DP) in one-dimensional space; For (INT I = 1; I <= N; ++ I) {Len = 0; for (Int J = N; j> = I; -- j) {if (a [I] = A [J]) {DP [J] = Len + 1; if (j> I) {max = max (max, DP [J] * 2);} else {max = max (max, DP [J] * 2-1) ;}} else {if (a [I]> A [J]) {Len = max (Len, DP [J]) ;}}} printf ("% d \ n", max) ;}int main () {int t; scanf ("% d ", & T); While (t --) {scanf ("% d", & N); For (INT I = 1; I <= N; ++ I) {scanf ("% d", A + I) ;}solve () ;}return 0 ;}

 

Appendix: Longest Common ascending subsequence Algorithm

O (N ^ 2) algorithm of the longest common ascending subsequence (lcis)
Prerequisites: the basic idea of dynamic planning, LCs, Lis.
Problem: For string a and string B, obtain the lcis (longest common ascending subsequence) of A and B ).
First, we can see that this problem has many overlapping subproblems. So we thought about using DP. What is the top priority of DP? Define the status.
1. Define the state f [I] [J] to indicate the length of lcis composed of the first J characters of string B and ended with B [J.
Why is this not another State definition? The most important reason is that I only know this. Another reason is that I know that this definition can produce square algorithms. The only reason for this is that the state definition is too easy to use. I will talk about this later.
Let's take a look at this status. It seems a little tricky to think about the status that can be transferred. It is much easier if you reverse your thinking and check which status the optimal value of this status depends on. What status does this status depend on?
First, go to a [I]! = B [J] f [I] [J] = f [I-1] [J]. Why? F [I] [J] Is lcis ending with B [J]. If f [I] [J]> 0, it indicates a [1]. A [I] must have a character a [k] equal to B [J] (What if f [I] [J] is equal to 0? The value assignment has no effect ). Because a [k]! = A [I], then a [I] has no contribution to f [I] [J], so we can get the optimal value of F [I] [J] without considering it. So in a [I]! F [I] [J] = f [I-1] [J]. For more information, see How to Handle LCS.
What if a [I] = B [J? First, this is equal to at least the lcis with a length of 1. Then we need to find the longest lcis that can allow B [J] to connect to its end. Where was the longest lcis? First we need to find the f array of the first dimension must be the I-1. Because I has been paired with B [J], it cannot be used. And it cannot be a I-2, because the I-1 must be better than the I-2. What about the second dimension? Then you need to enumerate B [1] .. B [J-1], because you do not know which is the longest and which is less than B [J]. There is another problem, but is it possible not to pair? That is, in the case of a [I] = B [J], do you need to consider the decision of F [I] [J] = f [I-1] [J? The answer is no. Because if B [J] is not paired with a [I], it is the same as the previous A [1] .. A [J-1] pairing (Assuming f [I-1] [J]> 0, equal to 0 is not considered), this will inevitably not be superior to a [I] pairing. (Why? Because the transfer after pairing B [J] And a [I] Is max (F [I-1] [k]) + 1, pairing with the previous I 'is max (F [I'-1] [k]) + 1. Obviously, F [I] [J]> F [I '] [J], I'> I)
So we come up with the state transition equation:
A [I]! = B [J]: F [I] [J] = f [I-1] [J]
A [I] = B [J]: F [I] [J] = max (F [I-1] [k]) + 1 1 <= k <= J-1 & B [J]> B [k]
It is easy to see that this is a time complexity of O (N ^ 3) DP, there is still a distance from the square.
However, the most important thing about this algorithm is, if we follow a reasonable recursive order, max (F [I-1] [k]) you can obtain the value of F [I] [k] by updating a Max Variable during maintenance. How can this problem be solved? First, the recurrence sequence must be the first dimension of the state in the outer loop, and the second dimension in the inner layer. That is, after calculating f [1] [Len (B)], then calculate f [2] [1].
In this recursive order, we can add a Max Variable to 0 at the beginning of each outer loop, and then start the inner loop. When a [I]> B [J], max = f [I-1] [J]. If the loop reaches a [I] = B [J], F [I] [J] = MAX + 1.
The final answer is the maximum value of F [Len (a)] [1]. f [Len (a)] [Len (B.
Reference code:

#include<cstdio>#include<cstring>int f[1005][1005],a[1005],b[1005],i,j,t,n1,n2,max;int main(){    scanf("%d",&t);    while(t--)    {        scanf("%d%d",&n1,&n2);        for(i=1;i<=n1;i++) scanf("%d",&a[i]);        for(i=1;i<=n2;i++) scanf("%d",&b[i]);        memset(f,0,sizeof(f));        for(i=1;i<=n1;i++)        {            max=0;            for(j=1;j<=n2;j++)            {                f[i][j]=f[i-1][j];                if (a[i]>b[j]&&max<f[i-1][j]) max=f[i-1][j];                if (a[i]==b[j]) f[i][j]=max+1;            }        }        max=0;        for(i=1;i<=n2;i++) if (max<f[n1][i]) max=f[n1][i];        printf("%d\n",max);    }}

 

In fact, there is also a very cool one-dimensional algorithm. On this basis, the one-dimensional space (time or square) is squashed ). When I loops to X, F [I] indicates the original f [x] [J]. This is because if a [I]! = B [J], because f [x] [J] = f [x-1] [J] value does not change, F [x] does not need to change, just keep the past, we still need the max value from this maintenance update. When a [I] = B [J], the value of F [x] is changed. Specific understanding in combination with the code.
Reference code:

#include<cstdio>#include<cstring>int f[1005],a[1005],b[1005],i,j,t,n1,n2,max;int main(){    scanf("%d",&t);    while(t--)    {        scanf("%d%d",&n1,&n2);        for(i=1;i<=n1;i++) scanf("%d",&a[i]);        for(i=1;i<=n2;i++) scanf("%d",&b[i]);        memset(f,0,sizeof(f));        for(i=1;i<=n1;i++)        {            max=0;            for(j=1;j<=n2;j++)            {                if (a[i]>b[j]&&max<f[j]) max=f[j];                if (a[i]==b[j]) f[j]=max+1;            }        }        max=0;        for(i=1;i<=n2;i++) if (max<f[i]) max=f[i];        printf("%d\n",max);    }}

The square algorithm of the longest common ascending subsequence (lcis) @ We all love Liu rujia
2011-2-18

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.