动态规划(4)——最长上升子序列(NYOJ17单调递增最长子序列)

单调递增最长子序列

描述
求一个字符串的最长递增子序列的长度
如:dabdbf最长递增子序列就是abdf,长度为4
输入
第一行一个整数0<n<20,表示有n个字符串要处理
随后的n行,每行有一个字符串,该字符串的长度不会超过10000
输出
输出字符串的最长递增子序列的长度
样例输入
3
aaa
ababc
abklmncdefg
经典方法求单调上升子序列的最大长度。
AC代码:
 
//最长单调递增子序列
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
using namespace std;
int dp[10005];

int main()
{
    int n;
    scanf("%d",&n);
    while(n--)
    {
        string str;
        cin>>str;
        memset(dp,0,sizeof(dp));
        dp[0]=1;
        for(int i=1;i<str.length();i++)
        {
            int ans=0;
            for(int j=0;j<i;j++)
            {
                if(str[i]>str[j]&&ans<dp[j])
                    ans=dp[j];
            }        
            dp[i]=ans+1;
        }
        int re=0;
        for(int i=0;i<str.length();i++)
        {
            if(re<dp[i])
                re=dp[i];        
        }
        printf("%d
",re);
    }    

    return 0;
} 
        


 
原文地址:https://www.cnblogs.com/xueniwawa/p/3745589.html