1481 魔族密码
貌似这个题是求最长上升序列
貌似这个题用模拟就能求出来了
dp[i]表示最长上升序列的长度
然后只要能查找子串,就更新答案
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
using namespace std;
const int maxn=2010;
int n,ans,dp[maxn];
string s[maxn];
int main()
{
cin>>n;//输入
for (int i=1;i<=n;i++)
{
cin>>s[i];
dp[i]=1;//初始化,dp[i]表示最长上升序列的个数
for (int j=1;j<i;j++)
{
if (s[i].find(s[j])==0)//查找子串
{
dp[i]=max(dp[i],dp[j]+1);//增加方案
}
}
ans=max(ans,dp[i]);//比较出最大的方案
}
cout<<ans<<endl;
return 0;
}
本文介绍了一种利用动态规划算法求解最长上升子序列(LIS)的方法。通过遍历字符串并查找子串,更新每个位置的最长上升序列长度,最终找出全局最大值。代码中展示了如何实现这一过程。
484

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



