Longest Increasing Subsequence - LIS变形
程序员文章站
2022-03-12 21:41:11
...
思路:
dp[i][j]:=以i结尾的长度为j的LIS的个数
若a[j]>a[i],dp[i][j]+=dp[i-1][j-1]
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<queue>
#include<set>
#define ll long long
using namespace std;
int a[105],dp[105][105];
const int mod=1e9+7;
int main(){
int t,n,k;
scanf("%d",&t);
while(t--){
memset(dp,0,sizeof(dp));
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
dp[i][1]=1;
}
for(int i=1;i<=n;i++){//当前位置
for(int j=2;j<=i;j++){//LIS长度
for(int q=1;q<i;q++){//判断当前位置之前的位置的所有情况
if(a[q]<a[i]){
dp[i][j]=(dp[i][j]+dp[q][j-1])%mod;
}
}
}
}
int ans=0;
for(int i=1;i<=n;i++){//求长度为k的LIS个数
ans=(ans+dp[i][k])%mod;
}
printf("%d\n",ans);
}
}
推荐阅读
-
最长上升子序列(LIS: Longest Increasing Subsequence)
-
【dp-LIS】牛客网 --最长上升子序列 POJ 2533--Longest Ordered Subsequence(LIS模板题)
-
最长递增子序列(Longest Increasing Subsequence)
-
最长上升子序列(LIS: Longest Increasing Subsequence)
-
最长上升子序列 Longest Increasing Subsequence 输出其中一个序
-
Longest Increasing Subsequence最长增长子序列
-
Number of Longest Increasing Subsequence
-
300. Longest Increasing Subsequence
-
Longest Increasing Subsequence
-
Number of Longest Increasing Subsequence