方法一:使用最长公共子序列的方法(实际上是最长不减子序列)
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
//最长递增子序列,例1357674->13567
int main()
{
string str;
string str2="";
int dp[100][100]={0};
for(int i=0;i<100;i++)
for(int j=0;j<100;j++)
dp[i][j]==0;
cin>>str;
str2+=str;
int len=str2.length();
for(int i=0;i<len;i++)
for(int j=i;j<len;j++)
{
if(str2[i]>str2[j])
{
char temp=str2[i];
str2[i]=str2[j];
str2[j]=temp;
}
}
for(int i=1;i<=len;i++)
for(int j=1;j<=len;j++)
{
if(str[i-1]==str2[j-1])
dp[i][j]=dp[i-1][j-1]+1;
else
dp[i][j]=max(dp[i][j-1],dp[i-1][j]);
}
cout<<dp[len][len]<<endl;
}
方法二:动态规划(其中dp[i]是指以str[i]为结尾的最长递增子序列)
#include<iostream>
#include<string>
#include<algorithm>
#define MAX 1000000
using namespace std;
//最长递增子序列
int main()
{
string str;
int ans=0;
cin>>str;
int dp[100]={1};
int len=str.length();
for(int i=0;i<len;i++)
for(int j=0;j<i;j++)
{
if(str[i]>str[j]&&dp[j]>=dp[i])
dp[i]=dp[j]+1;
}
for(int i=0;i<len;i++)
if(dp[i]>ans)
ans=dp[i];
cout<<ans<<endl;
}