J - 单调递增最长子序列
Description
求一个字符串的最长递增子序列的长度
如:dabdbf最长递增子序列就是abdf,长度为4
如:dabdbf最长递增子序列就是abdf,长度为4
Input
第一行一个整数0<n<20,表示有n个字符串要处理
随后的n行,每行有一个字符串,该字符串的长度不会超过10000
Output
输出字符串的最长递增子序列的长度
Sample Input
3 aaa ababc abklmncdefg
Sample Output
1 3 7
该题看似棘手,但是当用到lower_bound的函数时,便简单许多:
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int main()
{
int t;
char a[100521];
scanf("%d%*c",&t);
while(t--)
{
scanf("%s",a);
getchar();
int count = 0;
char b[100521];
b[0]=a[0];
int ans= 1;
int l = strlen(a);
for(int i = 0;i < l;i++)
{
if(a[i]>b[ans-1])
{
b[ans]=a[i];
ans++;
}
else
{
int n=lower_bound(b,b+ans,a[i])-b;
b[n]=a[i];
}
}
//zfghiabc//
printf("%d\n",ans);
}
return 0;
}愿你一生清澈明朗,做你愿做之事,爱你愿爱之人!
本文介绍了一种求解最长递增子序列长度的问题,并提供了一个使用C++实现的示例代码,通过lower_bound函数简化了解决方案。
1020

被折叠的 条评论
为什么被折叠?



