最长公共上升子序列——hdu1423 Greatest Common Increasing Subsequence
来源:互联网 发布:数据集市的作用 编辑:程序博客网 时间:2024/05/21 17:45
分析:dp(j)表示b序列到达i时与a序列构成的LCIS的最优解。
当a[i]==b[j]时,dp(j)为0到j-1中最大的dp值更新当前dp(j)。若a[i]>b[j],保存当前的最大的dp值,因为要上升,所以只需要找a[i] > b[j]的。若a[i]<b[j]则已经打破上升性(在b序列中),所以就不用记录当前最大的dp值
#include <bits/stdc++.h>using namespace std;const int maxn = 505;int a[maxn],b[maxn];int dp[maxn]; //dp(i)表示b序列到达i时与a序列构成的LCIS的最优解int n,m;void solve(){ memset(dp,0,sizeof(dp)); for(int i = 0;i < n;i++){ int k = 0; for(int j = 0;j < m;j++){ if(a[i] == b[j]) dp[j] = max(dp[j],dp[k]+1); if(a[i] > b[j]){ if(dp[j] > dp[k]) k = j; } } } int MAX = 0; for(int i = 0;i < m;i++){ MAX = max(dp[i],MAX); } printf("%d\n",MAX);}int main(){ int t; scanf("%d",&t); while(t--){ scanf("%d",&n); for(int i = 0;i < n;i++){ scanf("%d",&a[i]); } scanf("%d",&m); for(int i= 0;i < m;i++){ scanf("%d",&b[i]); } solve(); if(t) printf("\n"); } return 0;}
0 0
- hdu1423---Greatest Common Increasing Subsequence(最长公共上升子序列)
- 最长公共上升子序列——hdu1423 Greatest Common Increasing Subsequence
- Greatest Common Increasing Subsequence hdu1423 最长公共递增子序列
- HDU1423&ZOJ2432 - Greatest Common Increasing Subsequence(LCIS最长公共上升子序列模板)
- Greatest Common Increasing Subsequence-最长公共上升子序列
- Greatest Common Increasing Subsequence(最长单调递增公共子序列+hdu1423)
- hdu1423 Greatest Common Increasing Subsequence(最长递增公共子序列)
- HDU1423 Greatest Common Increasing Subsequence(最长公共递增子序列)
- hdu 1423 Greatest Common Increasing Subsequence (最长上升子序列)
- hdu 1423 Greatest Common Increasing Subsequence (最长上升子序列)
- hdu 1423 Greatest Common Increasing Subsequence 最长公共上升子序列
- HDU 1423 Greatest Common Increasing Subsequence(最长公共上升子序列)
- hdu 1423 Greatest Common Increasing Subsequence(DP 最长公共上升子序列)
- POJ 2127 Greatest Common Increasing Subsequence (最长公共上升子序列+记录路径)
- hdu 1423 Greatest Common Increasing Subsequence(最长上升公共子序列)
- hdu 1423 Greatest Common Increasing Subsequence(最长公共上升子序列、LCIS)
- zoj 2432 Greatest Common Increasing Subsequence(最长公共上升子序列)
- HDU OJ 1423Greatest Common Increasing Subsequence 最长公共上升子序列
- iOS开发学习之C语言---C10 函数指针-1
- Android 怎样把底部栏顶上去
- 【转】java基础:堆&栈
- iOS开发学习之C语言---C10 函数指针-2
- leetcode Problem1 two sum
- 最长公共上升子序列——hdu1423 Greatest Common Increasing Subsequence
- static 剖析(C++)
- JSP/Servlet-----10、JSP2的自定义标签
- iOS开发学习之C语言---C10 函数指针-3
- iOS开发学习之C语言---C11 二维数组、字符串数组、多维数组
- 值得推荐的C/C++框架和库
- leetcode15 3Sum
- 排序算法之Bucket Sort
- 二维数组查询