2016-level algorithm final simulation exercise-F.ALVINZH Youth Memory IV

Source: Internet
Author: User
Tags array definition

1086 Alvinzh's Youth Memory IV thinking

Challenges, dynamic planning.

This is a very interesting question, because it not only card the time, also card space, and the card is very good and fascinated.

Just understanding test instructions has been a bit difficult, simplifying test instructions: Two series of numbers, equal numbers defined as can be engaged, if AI==BJ, then Ai can fight with Bj, then the next Aii, BJJ the conditions are: AII==BJJ, Ii>i, Jj>j, Aii>ai, BJJ>BJ.

What is this? Longest common sub-sequence? The longest increment of a subsequence? Are very much like, in fact, the longest public increment subsequence ! The longest common subsequence is superimposed on the longest increment subsequence, which is Lcs+lis, called the LCIs problem.

If you do not know how to ask for LCS and LIS, please review and continue to look down.

Before you start talking about Tle, let's talk about the MLE problem. You can see that the maximum number of \ (n\) given in the topic is 1E4, if your \ (dp\) Array is a two-dimensional 1e4 array of int, it obviously exceeds the space limit.
Here to think of a \ (dp\) array definition, it represents the size of LCIs, this size is not possible to exceed \ (n\) and \ (m\) , so dp[i][j]<1e4, so the DP array is defined as \ ( Short\ int\).
Note A small problem: math function max (int,int), which needs to be converted to int before size.

Method One: The most primitive method--total violence

\ (dp[i][j]\): a The longest common ascending subsequence of a former I-element and B-First-j element.

Four for loop, outer two to judge LCS, inner layer two to Judge Lis. TLE, needless to say.

The code below, time complexity: O (n^4), spatial complexity: O (n^2). Can get 0.4 points, TLE.

int n, m, ans;int A[10005];int B[10005];short int dp[10005][10005];//dp[i][j]:A前i个元素和B前j个元素最长公共上升子序列ans = 0;memset(dp, 0, sizeof(dp));for(int i = 1; i <= n; i++){    for(int j = 1; j <= m; j++)    {        if(A[i] == B[j])        {            for(int k = 0; k < i; k++)                if(A[k] < A[i])                    for(int l = 0;l < j; l++)                        dp[i][j] = max((int)dp[i][j], (int)dp[k][l]+1);        }        ans = max(ans, (int)dp[i][j]);    }}printf("%d\n", ans);//完全暴力//时间复杂度:O(n^4)//空间复杂度:O(n^2)
Method Two: Undo and Redo, redefine the DP array

It can be found that the DP array definition in the previous method is simply too common, and we can limit it.

\ (dp[i][j]\): The length of the LCIs consisting of the first and second integers of a string and the first j integers of string B and the end of B[j], that is, the length of the b[j] is not forced to select A[i].

State transition equation:

    • ①DP[I][J] = Dp[i-1][j]. (A[i]! = B[j])
    • ②DP[I][J] = max (Dp[i-1][k]) +1 (a[i] = B[j]) (1 <= k <= j-1 && b[j] > B[k])

For ①,DP[I][J] Definitions: b[j] must be used. But a[i]! = B[j],a[i] No contribution, can be directly taken dp[i-1][j].

For ②: Two number equal case for the longest, b[j] must match a[i], if the previous a[1~i-1] match, there must be no better than with A[i] match. Next you need to find an increment of b[j] end of LCIs, enumeration dp[i-1][1~j-1] Find the maximum value. Why the first dimension is i-1,a[i] has been matched, and taking [1~i-2] is obviously no better than taking i-1.

Reference Code One: the most superficial translation

According to the above DP definition can be directly translated, get the most obvious solution code as follows, still timeout, can get 0.4 points, TLE.

Complexity of Time: O (n^3), spatial complexity: O (n^2).

int n, m, ans;int A[10005];int B[10005];short int dp[10005][10005];//dp[i][j]:以A串的前i个整数与B串的前j个整数且以B[j]为结尾构成的LCIS的长度ans = 0;memset(dp, 0, sizeof(dp));for(int i = 1; i <= n; i++){    for(int j = 1; j <= m; j++)    {        if(A[i] == B[j])        {            int maxx = 0;            for(int k = 1; k <= j-1; k++)//找到符合递增的最大长度            {                if(B[j] > B[k])//保证递增                    maxx = max(maxx, (int)dp[i-1][k]);            }            dp[i][j] = maxx + 1;        }        else            dp[i][j] = dp[i-1][j];    }}for(int j = 1; j <= m; j++)  if(ans < dp[n][j]) ans = dp[n][j];//时间复杂度:O(n^3)//空间复杂度:O(n^2)

Reference code two: time optimization of code one

Time Optimization: The Maxx can be evaluated directly in the second layer, without the need for a third loop. How to obtain?

First understand the definition of Maxx: Maxx=max (Dp[i-1][k]), k=[1,j-1]. Refers to the LCIs that is formed in accordance with the ascending condition (B[k]

Because B[j] has been matched with A[i], looking for b[j] > b[k], i.e. launch a[i] > B[k]. The value of the Maxx can be updated directly in the second-level loop.

The code below, time complexity: O (n^2), spatial complexity: O (n^2). Can get 0.6 points, TLE.

int n, m, ans;int A[10005];int B[10005];short int dp[10005][10005];//dp[i][j]:以A串的前i个整数与B串的前j个整数且以B[j]为结尾构成的LCIS的长度ans = 0;memset(dp, 0, sizeof(dp));for(int i = 1; i <= n; i++){    int maxx = 0;    for(int j = 1; j <= m; j++)    {        if(A[i] == B[j])            dp[i][j] = maxx+1;        else            dp[i][j] = dp[i-1][j];        if(A[i] > B[j])            maxx = max(maxx, (int)dp[i-1][j]);    }}for(int j = 1; j <= m; j++)    if(ans < dp[n][j]) ans = dp[n][j];//时间复杂度:O(n^2)//空间复杂度:O(n^2)

Reference code three: space optimization for code two

The DP update is found to be related only to the previous state, so the DP array can become DP[2][10005], the bit operation is updated, and the rest is unchanged.

The code below, time complexity: O (n^2), spatial complexity: O (n). Can be 1 points, AC. Finally, it can be AC, but why?

Inexplicable AC, you should ask why. Code two and code three contrast, just space is smaller, for the second set of data, why a 1500ms direct T, and this 36ms brush past it? What's the hole in the second set of data?

I'm straight: n is small in the second set of data, and AI and bi are small, but the number of data groups is a bit large. One reason can be understood as the initialization problem, the second reason is the heap space allocation problem, see how your group learning!

int n, m, ans;int A[10005];int B[10005];short int dp[2][10005];ans = 0;memset(dp, 0, sizeof(dp));int cur = 0;for(int i = 1; i <= n; i++){    cur = cur^1;    int maxx = 0;    for(int j = 1; j <= m; j++)    {        if(A[i] == B[j])            dp[cur][j] = maxx+1;        else            dp[cur][j] = dp[cur^1][j];        if(A[i] > B[j])            maxx = max(maxx, (int)dp[cur^1][j]);    }}for(int j = 1; j <= m; j++)    if(ans < dp[cur][j]) ans = dp[cur][j];printf("%d\n", ans);//时间复杂度:O(n^2)//空间复杂度:O(n)

Reference code four: further optimization of code three

What the!!! can also be optimized!

Yes, in fact, since it is possible to operate a scrolling optimization, it may become a one-dimensional array just like a 01 backpack. This is really the way to do it.

The definition of a DP array changes slightly, Dp[i] indicates that the range of lcis,a ending with b[i] is [1~n].

The code below, time complexity: O (n^2), spatial complexity: O (n). Can be 1 points, AC.

int n, m, ans;int A[10005];int B[10005];short int dp[10005];//dp[i]表示以B[i]结尾的LCIS,A的范围是[1~n]ans = 0;memset(dp, 0, sizeof(dp));for(int i = 1; i <= n; i++){    int maxx = 0;    for(int j = 1; j <= m; j++)    {        if(A[i] == B[j])            dp[j] = maxx+1;        if(A[i] > B[j])            maxx = max(maxx, (int)dp[j]);    }}for(int j = 1; j <= m; j++)    if(ans < dp[j]) ans = dp[j];//时间复杂度:O(n^2)//空间复杂度:O(n)
Analysis

No analysis, also analyze a yarn! Can only say, we have no knowledge of the power of DP!

2016-level algorithm final simulation exercise-F.ALVINZH Youth Memory IV

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.