POJ2127 Greatest Common Increasing Subsequence
给定两个 整数序列,求LCIS(最长公共上升子序列)
dp[i][j]表示A的A[1.....i]与B[1.....j]的以B[j]为结尾的LCIS。
转移方程很简单
当A[i]!=B[j] dp[i][j]=dp[i-1][j]
else dp[i][j]=max(dp[i][k]+1) k<j A[i]>B[k]
朴素实现O(n^3)
通过标记最大值的方法可以优化到O(n^2)
代码很简单
#include<stdio.h>
#include<stdlib.h>
#include<string>
#include<vector>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=500+5;
int dp[maxn][maxn];
int pre[maxn][maxn];
int A[maxn],B[maxn];
int n1,n2;
vector<int> getLcis()
{
memset(dp,0,sizeof(dp));
memset(pre,0,sizeof(pre));
vector<int>lcis;
for(int i=1;i<=n1;i++)
{
int k=0;
for(int j=1;j<=n2;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;
}
}
}
int ans=-1,x=n1,y=0;
for(int i=1;i<=n2;i++)
{
if(dp[n1][i]>ans)
{
ans=dp[n1][i];
y=i;
}
}
lcis.resize(ans);
int cnt=1;
while(dp[x][y])
{
if(A[x]!=B[y])x--;
else{
lcis[ans-cnt]=B[y];
cnt++;
y=pre[x][y];
}
}
return lcis;
}
int main()
{freopen("t.txt","r",stdin);
scanf("%d",&n1);
for(int i=1;i<=n1;i++)
scanf("%d",&A[i]);
scanf("%d",&n2);
for(int i=1;i<=n2;i++)
scanf("%d",&B[i]);
vector<int>L=getLcis();
printf("%d\n",L.size());
for(int ii=0;ii<(int)(L.size()-1);ii++)
if(ii<L.size())printf("%d ",L[ii]); if(L.size()>0)printf("%d\n",L[L.size()-1]);
return 0;
}
POJ2127 Greatest Common Increasing Subsequence的更多相关文章
- HDU 1423 Greatest Common Increasing Subsequence LCIS
		
题目链接: 题目 Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...
 - POJ 2127 Greatest Common Increasing Subsequence -- 动态规划
		
题目地址:http://poj.org/problem?id=2127 Description You are given two sequences of integer numbers. Writ ...
 - HDOJ 1423 Greatest Common Increasing Subsequence -- 动态规划
		
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1423 Problem Description This is a problem from ZOJ 2 ...
 - ZOJ 2432 Greatest Common Increasing Subsequence(最长公共上升子序列+路径打印)
		
Greatest Common Increasing Subsequence 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problem ...
 - HDU 1423 Greatest Common Increasing Subsequence(最长公共上升LCIS)
		
HDU 1423 Greatest Common Increasing Subsequence(最长公共上升LCIS) http://acm.hdu.edu.cn/showproblem.php?pi ...
 - HDU1423:Greatest Common Increasing Subsequence(LICS)
		
Problem Description This is a problem from ZOJ 2432.To make it easyer,you just need output the lengt ...
 - Greatest Common Increasing Subsequence   hdu1423
		
Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536 ...
 - POJ 2127 Greatest Common Increasing Subsequence
		
You are given two sequences of integer numbers. Write a program to determine their common increasing ...
 - HDUOJ ---1423 Greatest Common Increasing Subsequence(LCS)
		
Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536 ...
 
随机推荐
- nginx配置文件+本地测试请求转发到远程服务器+集群
			
1 在本地测试1 众所周知,nginx是一个反向代理的服务器,主要功能即为实现负载均衡和动静分离.在别的我别的文章有详细的nginx(Windows)相关介绍教程. 由于自己安装的nginx在本地的计 ...
 - Courses on Turbulence
			
Courses on Turbulence Table of Contents 1. Lecture 1.1. UIUC Renewable energy and turbulent environm ...
 - 【】node基础概念问题(转载)
			
1.nodejs编写HelloWord,了解什么是nodejs,nodejs有什么特点 2.nodejs的模块怎么用,如何载入别的模块(require),如何给另一模块调用(module, mod ...
 - 【转】Java的path,classpath,java_home环境变量的配置与具体含义
			
对于一个Java初学者来说,第一步要做的是安装jdk并配置环境变量,一般按照书上或者网上的步骤,一步步照着做就行了,但是对于初学者来说,很多问题没有解决,比如为什么很多配置方法各不相同,却都能够配置成 ...
 - 确定位置的经纬度LocationUtil
			
package com.pingyijinren.test; import android.content.Context; import android.location.Location; imp ...
 - Codeforces Round #457 (Div. 2) B
			
B. Jamie and Binary Sequence (changed after round) time limit per test 2 seconds memory limit per te ...
 - oracle11g expdp/impdp数据库
			
oracle11g导入/导出数据库 导出 .创建目录 sqlplus / as sysdba create directory dbDir as 'd:\oralce_sdic_backup\'; g ...
 - ETL全量多表同步简述
			
ETL全量多表同步简述 1. 实现需求 当原数据库的表有新增.更新.删除操作时,将改动数据同步到目标库对应的数据表. 2. 设计思路 设计总体流程图如下: 1.获取同步表名如下图: 2.循环迁移数据如 ...
 - Eclipse配色方案插件 真漂亮!
			
原文:https://my.oschina.net/jean/blog/208263 最近发现了一个Eclipse配色方案插件,这回给Eclipse配色太方便了. 插件主页:http://eclips ...
 - 013 IPv6
			
Router>en Router#config t Enter configuration commands, one per line. End with CNTL/Z. Router(co ...