Monotonically increasing longest subsequence
Time Limit:
3000 ms | Memory Limit: 65535 KB
Difficulty:
4
- describe
-
Find the length of the longest increasing subsequence of a string
For example : dabdbf The longest increasing subsequence is abdf, with a length of 4
- enter
-
The first line contains an integer 0<n<20, indicating that there are n strings to be processed.
The next n lines, each line has a string, and the length of the string will not exceed 10000 - output
- the length of the longest increasing subsequence of the output string
- sample input
-
3 aaa ababc abklmncdefg
- Sample output
-
1 3 7
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;int dp[10005];
int main()
{
int n;
cin>>n;
while(n--)
{
string s;
cin>>s;
memset(dp,0,sizeof(dp));
for(int i=1;i<s.length();++i)
{
for(int j=0;j<i;++j)
if(s[i]>s[j])
dp[i]=max(dp[i],dp[j]+1);
}
cout<<*max_element(dp,dp+s.length())+1<<endl;//+1 加上第一个字符 总共的长度
}
return 0;
}