POJ 2355(LIS)

A - Longest Ordered Subsequence
Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Description

A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence ( a1a2, ..., aN) be any sequence (ai1ai2, ..., aiK), where 1 <= i1 < i2 < ... < iK <= N. For example, sequence (1, 7, 3, 5, 9, 4, 8) has ordered subsequences, e. g., (1, 7), (3, 4, 8) and many others. All longest ordered subsequences are of length 4, e. g., (1, 3, 5, 8). 

Your program, when given the numeric sequence, must find the length of its longest ordered subsequence.

Input

The first line of input file contains the length of sequence N. The second line contains the elements of sequence - N integers in the range from 0 to 10000 each, separated by spaces. 1 <= N <= 1000

Output

Output file must contain a single integer - the length of the longest ordered subsequence of the given sequence.

Sample Input

7
1 7 3 5 9 4 8

Sample Output

4

题目大意:

  给你一个长度为n的字符串,让你求出这个串的最长的子序列。

解题思路:

  常见的dp题型:LIS

  定义状态:dp[i]表示以a[i]结尾的最长上升子序列的长度

  初始状态:dp[i] = 1

   状态转移方程:dp[i] = max(dp[i],dp[j]+1 );&&(j < i,a[j] <a[i] )的情况下。

代码:

 1 # include<cstdio>
 2 # include<iostream>
 3 # include<fstream>
 4 # include<algorithm>
 5 # include<functional>
 6 # include<cstring>
 7 # include<string>
 8 # include<cstdlib>
 9 # include<iomanip>
10 # include<numeric>
11 # include<cctype>
12 # include<cmath>
13 # include<ctime>
14 # include<queue>
15 # include<stack>
16 # include<list>
17 # include<set>
18 # include<map>
19 
20 using namespace std;
21 
22 const double PI=4.0*atan(1.0);
23 
24 typedef long long LL;
25 typedef unsigned long long ULL;
26 
27 # define inf 999999999
28 # define MAX 10000+4
29 
30 int a[MAX];
31 int dp[MAX];
32 
33 
34 int main(void)
35 {
36     int n;
37     while ( scanf("%d",&n)!=EOF )
38     {
39         int res = -1;
40         for ( int i = 0;i < n;i++ )
41         {
42             scanf("%d",&a[i]);
43         }
44         for ( int i = 0;i < n;i++ )
45         {
46             dp[i] = 1;
47             for ( int j = 0;j < i;j++ )
48             {
49                 if ( a[j] < a[i] )
50                 {
51                     dp[i] = max(dp[i],dp[j]+1 );
52                 }
53             }
54             res = max(res,dp[i]);
55         }
56         printf("%d
",res);
57     }
58 
59 
60     return 0;
61 }
原文地址:https://www.cnblogs.com/wikioibai/p/4430752.html