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 ...
随机推荐
- PHP:GD库 图片水印处理
文章来源:http://www.cnblogs.com/hello-tl/p/7592974.html <?php /** * 处理图片类 * 1.添加文字水印 * 2.添加图片水印 * 3.压 ...
- 基于vue实现模糊匹配(这里以邮箱模糊匹配为例,其他的模糊匹配都可以类比)
html部分(主要部分): js: data: methods: 效果图:
- codeforces 407 div1 B题(Weird journey)
codeforces 407 div1 B题(Weird journey) 传送门 题意: 给出一张图,n个点m条路径,一条好的路径定义为只有2条路径经过1次,m-2条路径经过2次,图中存在自环.问满 ...
- webstrom破解-webstrom2018.2.4破解方法(xjl456852原创)
方法一: 获取注册码: http://idea.lanyus.com/ 方法二: 使用破解补丁 放在安装目录的bin目录下,并且编辑bin目录下的文件 如果使用的32位的webstrom就编辑webs ...
- 快捷搜索框(UISearchBar)简单实现 swift
1.在故事板里面拖入一个搜索栏和一个的tableView.2.创建的ViewController,实现代理:UISearchBarDelegate,UITableViewDataSource,的UIT ...
- 全文搜索(AB-2)-权重
概念 权重是一个相对的概念,针对某一指标而言.某一指标的权重是指该指标在整体评价中的相对重要程度.权重是要从若干评价指标中分出轻重来,一组评价指标体系相对应的权重组成了权重体系. 释义 等同于比重 ...
- World is Exploding 树状数组+离散化
Given a sequence A with length n,count how many quadruple (a,b,c,d) satisfies: a≠b≠c≠d,1≤a<b≤n,1≤ ...
- HDU 5644 King's Pliot【费用流】
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5644 题意: 每天都有p[i]个飞行员进行阅兵,飞行员只工作一天. m个休假公式,花费tt[i]元让 ...
- ZOJ 1298_Domino Effect
题意: 多米诺骨牌效应:若干个关键牌相连,关键牌之间含有普通牌,关键牌倒下后其所在的行的普通牌全部倒下.求从推倒1号关键牌开始,最终倒下的牌的位置及时间. 分析: 最终倒下的牌的位置有两种情况,要么是 ...
- Memcached的几种Java客户端(待实践)
其实现在来尝试Memcached的客户端估计会有点过气,因为现在大势基本都在Redis那边. Memcached Client目前有3种: Memcached Client for Java(已经停止 ...