单调递增最长子序列
时间限制:
3000 ms | 内存限制:
65535 KB
难度:
4
-
描述
-
求一个字符串的最长递增子序列的长度
如:dabdbf最长递增子序列就是abdf,长度为4-
输入
-
第一行一个整数0<n<20,表示有n个字符串要处理
随后的n行,每行有一个字符串,该字符串的长度不会超过10000
输出
- 输出字符串的最长递增子序列的长度 样例输入
-
3 aaa ababc abklmncdefg
样例输出
-
1 3 7
-
第一行一个整数0<n<20,表示有n个字符串要处理
dp[i]代表以i结尾的最长递增子序列,dp[i]=max{dp[i-1],dp[i-2]......}+1;
#include<iostream>
#include<cstring>
#include<cstdio>
#include <algorithm>
using namespace std;
char s1[10005];
int dp[10005];
int n;
int main(){
int max;
scanf("%d",&n);
while(n--){
scanf("%s",s1);
int lenth = strlen(s1);
for(int i=0;i<lenth;i++){
dp[i]=1;
}
for(int i=1;i<lenth;i++){
for(int j=i-1;j>=0;j--){
if(s1[i]>s1[j]&&dp[i]<dp[j]+1){
dp[i]=dp[j]+1;
}
}
}
max = dp[0];
for(int i=1;i<lenth;i++){
if(dp[i]>max)max=dp[i];
}
printf("%d\n",max);
}
return 0;
}