hdu 1423 Greatest Common Increasing Subsequence 经典dp
来源:互联网 发布:翱奔qq营销软件 编辑:程序博客网 时间:2024/06/07 15:33
// 最长公共上升子序列问题// 以f[i][j]表示a串前i个字符与b串前j个字符并且以b[j]为结尾匹配的状况// 为什么定义这个状态呢,因为我也是看到的百度文库中@我们都爱刘汝佳// 这篇文章中的介绍,发现这个状态真的非常好用// 状态转移方程// a[i]!=b[j]那么f[i][j] = f[i-1][j],换而言之,我们可以直接丢掉a串中// 的a[i]不用考虑,这里和最长公共子序列的问题有点类似// a[i]==b[j]那么f[i][j] = max(f[i-1][k]+1){k<j&&b[k]<b[j]}// a[i]已经和b[j]配对了,则在a[0...i-1]与b[0...j-1]找最大的+1就是我们要的// 答案,我们的状态中有以b[j]为结尾这个定义,则我们只要找// 当a[i]>b[j]时f[i-1][j]这样的max+1就可以啦,(因为这样的a[i]是不被需要的)// 更加节省空间的做法就是用滚动数组// 这算是dp的经典问题啦,继续练吧。。。#include <algorithm>#include <bitset>#include <cassert>#include <cctype>#include <cfloat>#include <climits>#include <cmath>#include <complex>#include <cstdio>#include <cstdlib>#include <cstring>#include <ctime>#include <deque>#include <functional>#include <iostream>#include <list>#include <map>#include <numeric>#include <queue>#include <set>#include <stack>#include <vector>#define ceil(a,b) (((a)+(b)-1)/(b))#define endl '\n'#define gcd __gcd#define highBit(x) (1ULL<<(63-__builtin_clzll(x)))#define popCount __builtin_popcountlltypedef long long ll;using namespace std;const int MOD = 1000000007;const long double PI = acos(-1.L);template<class T> inline T lcm(const T& a, const T& b) { return a/gcd(a, b)*b; }template<class T> inline T lowBit(const T& x) { return x&-x; }template<class T> inline T maximize(T& a, const T& b) { return a=a<b?b:a; }template<class T> inline T minimize(T& a, const T& b) { return a=a<b?a:b; }const int maxn = 512;int a[maxn];int b[maxn];int f[maxn][maxn];int dp[maxn];int n,m;void init(){scanf("%d",&n);for (int i=1;i<=n;i++)scanf("%d",&a[i]);scanf("%d",&m);for (int i=1;i<=m;i++)scanf("%d",&b[i]);memset(f,0,sizeof(f));memset(dp,0,sizeof(dp));}void solve(){int mx ;for (int i=1;i<=n;i++){mx = 0;for (int j=1;j<=m;j++){f[i][j] = f[i-1][j];if (a[i]>b[j])mx = max(mx,f[i][j]);if (a[i]==b[j])f[i][j] = mx+1;}}mx = -1;for (int i=1;i<=m;i++)mx = max (mx , f[n][i]);printf("%d\n",mx);}void solve1(){int mx;for (int i=1;i<=n;i++){mx = 0;for (int j=1;j<=m;j++){if (a[i]>b[j])mx = max(dp[j],mx);if (a[i]==b[j])dp[j] = mx+1;}}mx = 0;for (int i=1;i<=m;i++)mx = max(mx,dp[i]);printf("%d\n",mx);}int main() {int t; freopen("G:\\Code\\1.txt","r",stdin);scanf("%d",&t);while(t--){init();solve1();if (t)puts("");}return 0;}
0 0
- hdu 1423 Greatest Common Increasing Subsequence 经典dp
- HDU 1423 Greatest Common Increasing Subsequence(DP)
- HDU 1423 Greatest Common Increasing Subsequence(dp)
- 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
- hdu 1423 Greatest Common Increasing Subsequence
- HDU 1423 Greatest Common Increasing Subsequence
- HDU 1423 Greatest Common Increasing Subsequence
- hdu 1423 Greatest Common Increasing Subsequence(LIS)
- HDU 1423--Greatest Common Increasing Subsequence【LCIS】
- HDU 1423 Greatest Common Increasing Subsequence
- HDU 1423 Greatest Common Increasing Subsequence
- HDU 1423 Greatest Common Increasing Subsequence
- HDU 1423 Greatest Common Increasing Subsequence(LCIS)
- HDU 1423 Greatest Common Increasing Subsequence
- hdu 5154 Harry and Magical Computer
- 杭电ACM 2004:成绩转换
- iOS- 如何集成支付宝
- 黑马程序员_java基础自学11
- 第九届 北邮程序设计大赛网络赛 部分题解
- hdu 1423 Greatest Common Increasing Subsequence 经典dp
- Spring 日志输出有关问题解决
- BZOJ2055【上下界费用流】
- hibernate注解配置
- 打印1到最大的n位数
- 新手成长记录
- android - Actionbar 上的 MenuItem 的 使用细节和优化
- qml编程规范--import与as
- How to log in Spring with SLF4J and Logback