1423 greatest common increasing subsequence (lcis)

Source: Internet
Author: User

The explanation is taken from Baidu;

What is the O (N ^ 2) algorithm of the longest common ascending subsequence (lcis?

Prerequisites: the basic idea of dynamic planning, LCs, Lis .?
Question: 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 I-1 must be better than 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. Apparently, 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 not hard 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], then 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", & 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 unchanged,
F [x] does not need to be changed. We just need to keep the previous one. We still need to maintain and update the max value.
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", & 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 );
}
Return 0;
}

Code:
# Include <stdio. h>
# Include <string. h>
# Deprecision num 1000
Int MAX [num] [num], flag [num];
Int main ()
{
Int I, j, n, m, T, R;
Int str1 [num], str2 [num], ans;
Scanf ("% d", & T );
While (t --)
{
Memset (max, 0, sizeof (max ));
Scanf ("% d", & N );
For (I = 1; I <= N; I ++)
Scanf ("% d", & str1 [I]);
Scanf ("% d", & M );
For (j = 1; j <= m; j ++)
Scanf ("% d", & str2 [J]);
For (I = 1; I <= N; I ++)
{
R = 0;
For (j = 1; j <= m; j ++)
{
Max [I] [J] = MAX [I-1] [J];
If (str1 [I]> str2 [J] & R <MAX [I-1] [J]) r = MAX [I-1] [J];
If (str1 [I] = str2 [J]) Max [I] [J] = R + 1;

}
}
Ans = 0;
For (I = 1; I <= m; I ++)
If (MAX [N] [I]> ans)
Ans = MAX [N] [I];
Printf ("% d \ n", ANS );
If (t)
Printf ("\ n ");
}
Return 0;
}
Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1423

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.