HDU4512完美队形I && HDU1423 Greatest Common Increasing Subsequence (LCIS)

填坑的时候又到啦,校赛因为不会LCIS所以吃了大亏,这里要补起来。LCIS就是在两个串里找最长上升子序列,相关的博客有很多,这里自己就不写那么多了。

http://www.cnblogs.com/jackge/archive/2013/05/16/3081793.html

http://www.cnblogs.com/gj-Acit/p/3236384.html

上面两个博客对于O(n^2)的做法讲解的比较详细,大家可以参考一下。

贴两记代码

HDU1423

#pragma warning(disable:4996)

#include <iostream>

#include <cstring>

#include <string>

#include <vector>

#include <algorithm>

#include <cstdio>

using namespace std;



#define maxn 550

int a[maxn];

int b[maxn];

int n1, n2;;

int dp[maxn][maxn];



int main()

{

    int T; cin >> T;

    while (T--){

        cin >> n1;

        for (int i = 1; i <= n1; i++) scanf("%d", a + i);

        cin >> n2;

        for (int i = 1; i <= n2; i++) scanf("%d", b + i);

        memset(dp, 0, sizeof(dp));

        for (int i = 1; i <= n1; i++){

            int tmp = 0;

            for (int j = 1; j <= n2; j++){

                dp[i][j] = dp[i - 1][j];

                if (a[i] > b[j] && dp[i - 1][j] > tmp) tmp = dp[i - 1][j];

                if (a[i] == b[j]) dp[i][j] = tmp + 1;

            }

        }

        int ans = 0;

        for (int i = 1; i <= n1; i++){

            ans = max(ans, dp[n1][i]);

        }

        printf("%d\n", ans);

        if (T) puts("");

    }

    return 0;

}

 HDU4512

#pragma warning(disable:4996)

#include <iostream>

#include <cstring>

#include <string>

#include <vector>

#include <algorithm>

#include <cstdio>

using namespace std;



#define maxn 550



int a[maxn];

int b[maxn];

int n;

int dp[maxn][maxn];



int main()

{

    int T; cin >> T;

    while (T--)

    {

        cin >> n;

        for (int i = 1; i <= n; i++){

            scanf("%d", a + i);

            b[n + 1 - i] = a[i];

        }

        int ans = 0;

        memset(dp, 0, sizeof(dp));

        for (int i = 1; i <= n; i++){

            int tmp = 0;

            for (int j = 1; j <= n+1-i; j++){

                dp[i][j] = dp[i - 1][j];

                if (a[i] > b[j] && dp[i - 1][j] > tmp) tmp = dp[i - 1][j];

                if (a[i] == b[j]){

                    dp[i][j] = max(dp[i][j],tmp + 1);

                }

                if (i < n + 1 - j) ans = max(ans, dp[i][j] * 2);

                else ans = max(ans, dp[i][j] * 2 - 1);

            }

        }

        printf("%d\n", ans);

    }

    return 0;

}

 

你可能感兴趣的:(sequence)