POJ 2533 POJ 1458 HUD 1423 dynamic programming LIS LCS LCIS

Rise up sequence
the TimeLimit: 1000MS the MemoryLimit: 64MB
64-Integer the IO 'bit the format:% LLD
Problem the Description
If a series ai satisfy a1 <a2 <... <aN then this series is called up sequencing.

Given a series a (a1, a2, ..., aN) is any of a number of columns b (ai1, ai2, ..., aiK) and satisfies (1 <= i1 <i2 <... <iK <= N). Then b is called It is a sub-sequence.

If a sequence number is rising column sequence, then this sequence is referred to increase sequence prosequence.

Such sequences (1, 7, 3, 5, 9, 4, 8) has a rising sequence (1, 7), (3, 4, 8) or the like which increase the longest sequence length is 4, i.e., ( 1, 3, 5, 8).

You are to write a program to find a sequence of sub-rise longest length of the sequence.

Input
The first line is an integer N represents the number of columns of a given length. The second row includes N an integer ranging from 0 to 10,000. 1 <= N <= 1000

Output
output a integer represents the maximum length of the longest sequence rise

SampleInput
7
1 7 3 5 9 4 8
SampleOutput
4

The meaning of problems: rising demand longest sequence length of
ideas: LIS board

#include <iostream>
#include <algorithm>

using namespace std;

const int N = 1005;
int a[N],dp[N];

int main()
{
    int n;

    cin >> n;

    for(int i=1;i<=n;i++)
    {
        cin >> a[i];
        dp[i]=1;///最少会有一个元素
    }

    for(int i=2;i<=n;i++)///外层循环记录子问题的答案
    {
        for(int j=1;j<i;j++)///遍历子问题
        {
            if(a[i]>a[j])///如果不满足条件
            {
                dp[i]=max(dp[i],dp[j]+1);///作出决策 在当前阶段和前一阶段中求最大值
            }
        }
    }

    cout << *max_element(dp+1,dp+n+1) << endl;///求出子问题答案的最大值

    return 0;
}

Longest common subsequence
the TimeLimit: 1000MS the MemoryLimit: 10MB
64-Integer the IO 'bit the format:% LLD
Problem the Description
Given two sequences X and Y, X and seeking common subsequence length of the maximum length of Y.

A sub-sequence as defined below, select a set of strictly increasing index <i1, i2, i3, i4, ...... im>

These new sequence on the elements constituting the subscript is the sub-sequence of the original sequence,

Sequence Z For example, after the sequence X = <a, b, c, f, b, c>, select index <1, 2, 4, 6> The resulting = <a, b, f, c> is the son of X one of the sequences

Input
The program input is from the std input. Each data set in the input contains two strings representing the given sequences. The sequences are separated by any number of white spaces. The input data are correct.

String length is less than equal to 1000

Output
For each set of data the program prints on the standard output the length of the maximum-length common subsequence from the beginning of a separate line.
SampleInput
abcfbc abfcab
programming contest
abcd mnp
SampleOutput
4
2
0
思路:LCS板子

#include <iostream>
#include <algorithm>

using namespace std;

const int N = 1005;

int dp[N][N];

int main()
{
    string s1,s2;

    while(cin >> s1 >> s2)
    {
        for(int i=0;i<=s1.size();i++)///将第一行第一列清零
            dp[i][0]=0;
        for(int i=0;i<=s1.size();i++)
            dp[0][i]=0;

        for(int i=1;i<=s1.size();i++)
        {
            for(int j=1;j<=s2.size();j++)
            {
                if(s1[i-1]==s2[j-1])///如果相等 则这一阶段的答案是上一阶段+1
                    dp[i][j]=dp[i-1][j-1]+1;
                else
                    dp[i][j]=max(dp[i-1][j],dp[i][j-1]);///如果不等  从上一阶段中求出更优秀的解
            }
        }

        cout << dp[s1.size()][s2.size()] << endl;
    }

    return 0;
}

The longest common subsequence increase
the TimeLimit: 1000MS the MemoryLimit: 32768MB
64 'bit-Integer the IO the format:% I64d
Problem the Description
This IS A problem from ZOJ 2432.To the make it easyer IT, you need Just The Output The length of subsequence.
The Input
first row T is an integer, representatives of groups of data T

Each sequence is described with M - its length (1 <= M <= 500) and M integer numbers Ai (-2^31 <= Ai < 2^31) - the sequence itself.

Output
output print L - the length of the greatest common increasing subsequence of both sequences.

Note that between each two sets of data to leave a blank line after the last set of data can not be empty lines

SampleInput
1

5
1 4 2 5 -12
4
-12 1 2 4
SampleOutput
2

LCIS
idea: that is modified from the base of the LCS + LIS

#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;

const int N = 505;

int a[N],b[N];
int dp[N][N];

void init()
{
    memset(a,0,sizeof(a));
    memset(b,0,sizeof(b));
    memset(dp,0,sizeof(dp));
}

int main()
{
    int t;

    cin >> t;

    while(t--)
    {
        init();

        int n,m;
        int temp=-100000;

        cin >> n;

        for(int i=1;i<=n;i++)
            cin >> a[i];

        cin >> m;

        for(int i=1;i<=m;i++)
            cin >> b[i];

        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                if(a[i]==b[j]&&a[i]>=temp)///两个值相同 并且是递增的则这一阶段答案+1
                {
                    dp[i][j]=dp[i-1][j-1]+1;
                    temp=a[i];
                }
                else
                {
                    dp[i][j]=max(dp[i-1][j],dp[i][j-1]);///否则 从上一阶段找出更优秀的解
                }
            }
        }

        cout << dp[n][m] << endl;
        if(t)
            cout << endl;
    }

    return 0;
}
Published 54 original articles · won praise 0 · Views 1225

Guess you like

Origin blog.csdn.net/weixin_44144278/article/details/99618392