POJ 2127 Greatest Common Increasing Subsequence
来源:互联网 发布:身高测试软件 编辑:程序博客网 时间:2024/05/18 17:58
Description
You are given two sequences of integer numbers. Write a program to determine their common increasing subsequence of maximal possible length.
Sequence S1 , S2 , … , SN of length N is called an increasing subsequence of a sequence A1 , A2 , … , AM of length M if there exist 1 <= i1 < i2 < … < iN <= M such that Sj = Aij for all 1 <= j <= N , and Sj < Sj+1 for all 1 <= j < N .
【题目分析】
最长公共上升子序列问题,只需要巧妙地转换一下思路,就可最长公共子序列差不多了。三种情况的转移和处理都十分巧妙
【代码】
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <cmath>#include <cstdlib>#include <ctime>using namespace std;int n,m,a[5001],b[5001];int dp[5001][5001],pre[5001][5001];int out[5001];int main(){// freopen("sail.in","r",stdin);// freopen("sail.out","w",stdout); 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]); int ans=0,now,cnt=0; for (int i=1;i<=n;++i) { int k=0; for (int j=1;j<=m;++j) { if (a[i]!=b[j]) dp[i][j]=dp[i-1][j]; if (a[i]>b[j]&&dp[i][j]>dp[i][k]) k=j; if (a[i]==b[j]) dp[i][j]=dp[i][k]+1,pre[i][j]=k; } }// for (int i=1;i<=n;++i)// {// for (int j=1;j<=m;++j)// cout<<dp[i][j]<<" ";// cout<<endl;// } int y=0,x=n; for (int i=1;i<=m;++i) if (ans<dp[n][i]) y=i,ans=dp[n][i]; while (dp[x][y]) { if (a[x]!=b[y]) x--; else out[++cnt]=a[x],y=pre[x][y]; } cout<<cnt<<endl; while (cnt>0) { if (cnt==1) printf("%d",out[cnt]); else printf("%d ",out[cnt]); cnt--; }}
0 0
- POJ 2127 Greatest Common Increasing Subsequence
- poj 2127 Greatest Common Increasing Subsequence
- poj 2127 Greatest Common Increasing Subsequence
- Greatest Common Increasing Subsequence poj 2127
- poj 2127 Greatest Common Increasing Subsequence
- POJ 2127 Greatest Common Increasing Subsequence
- POJ 2127 Greatest Common Increasing Subsequence
- POJ 2127 Greatest Common Increasing Subsequence
- POJ 2127 Greatest Common Increasing Subsequence
- POJ 2127 Greatest Common Increasing Subsequence 笔记
- POJ 2127 Greatest Common Increasing Subsequence (动态规划)
- POJ 2127 Greatest Common Increasing Subsequence -- 动态规划
- POJ 2127 Greatest Common Increasing Subsequence(LCIS+输出路径)
- poj 2127 Greatest Common Increasing Subsequence (LCIS)
- POJ 2127 Greatest Common Increasing Subsequence(DP,LCIS)
- poj 2127 Greatest Common Increasing Subsequence (记录路径LICS)
- POJ:2127 Greatest Common Increasing Subsequence(动态规划)
- Greatest Common Increasing Subsequence poj-1423
- 交叉排序
- HDOJ 1013 Digital Roots
- 【2016-CCPC-K】水题(Lweb and String,hdu 5842)
- POJ 1177 Pictures(HDU 1828) (线段树+离散化+线段扫描)
- http协议
- POJ 2127 Greatest Common Increasing Subsequence
- java redis 发送短信验证码
- 枚举Android系统的进程、任务和服务的信息
- 用Java实现最大公约数与最小公倍数
- 一个自己都没有用过的函数
- Linux常用命令整理
- robot基本变量
- JeeSite 对配置说明
- ReactiveCocoa入门教程——第一部分