单调递增最长子序列

描述
求一个字符串的最长递增子序列的长度
如:dabdbf最长递增子序列就是abdf,长度为4
输入
第一行一个整数0<n<20,表示有n个字符串要处理
随后的n行,每行有一个字符串,该字符串的长度不会超过10000
输出
输出字符串的最长递增子序列的长度
样例输入
3
aaa
ababc
abklmncdefg
样例输出
1
3
7
//动态规划问题
#include<stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include<string.h>
int main()
{
    char str[10000];
    int n;
    scanf("%d",&n);
    while(n--)
    {
        scanf("%s",&str);
        int m = strlen(str);
        int *book =(int *)malloc(m*sizeof(int));
        
        int k;
        for(k = 0;k<m;k++)
            book[k]=1;
        
        int count = 1;
        m--;
        while(m)
        {
            int i ;
            for(i = strlen(str)-1;i>m-1;i--)
            {
                if(str[m-1]<str[i])
                {
                    if(book[m-1]<=(book[i]+1))
                    {
                        book[m-1]=book[i]+1;
                    }    
                }        
            }
            if(count<book[m-1])
                count=book[m-1];
            m--;
        }
        printf("%d\n",count);
        
    }
    return 0;
} 

 

posted @ 2017-06-21 00:23  卡洛小豆  阅读(139)  评论(0编辑  收藏  举报