题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1423

好坑啊。。还有公共串为0时的特殊判断,还有格式错误。。看Discuss看知道除了最后一组测试数据之外都需要空行。。

其余的会LCS打印路径就行了。

法一:

///公共最长上升子序列
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#define N 505
using namespace std; int dp[N][N],flag[N][N];
int dp1[N];
int a[N],b[N],c[N];
int n,m,k; void solve_c(int i,int j){
if(i==||j==) return;
if(flag[i][j]==){
solve_c(i-,j-);
c[++k] = a[i];
}else if(flag[i][j]==){
solve_c(i-,j);
}else solve_c(i,j-);
}
void LCS(){
memset(flag,,sizeof(flag));
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
if(a[i]==b[j]){
dp[i][j] = dp[i-][j-]+;
flag[i][j]=;
}else{
if(dp[i-][j]>dp[i][j-]) flag[i][j]=;
else flag[i][j]=-;
dp[i][j] = max(dp[i-][j],dp[i][j-]);
}
}
}
//printf("%d",dp[n][m]);
k = ;
solve_c(n,m);
//for(int i=1;i<=k;i++) printf("%d ",c[i]);
}
void LIS(){
dp1[] = ;
int mx = ;
for(int i=;i<=k;i++){
dp1[i]=;
for(int j=;j<i;j++){
if(c[i]>c[j]&&dp1[i]<dp1[j]+){
dp1[i] = dp1[j]+;
}
if(mx<dp1[i]) mx=dp1[i];
}
}
if(k==) mx = ;
printf("%d\n",mx);
}
int main()
{
int tcase;
scanf("%d",&tcase);
while(tcase--){
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
}
scanf("%d",&m);
for(int i=;i<=m;i++){
scanf("%d",&b[i]);
}
memset(dp,,sizeof(dp));
LCS();
LIS();
if(tcase) printf("\n");
}
return ;
}

大神模板:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std; const int maxn = ;
int a[maxn],b[maxn],dp[maxn];
int n,m;
int LICS()
{
int i,j,MAX;
memset(dp,,sizeof(dp));
for(i = ; i<=n; i++)
{
MAX = ;
for(j = ; j<=m; j++)
{
if(a[i]>b[j] && MAX<dp[j])
MAX = dp[j];
if(a[i]==b[j])
dp[j] = MAX+;
}
}
MAX = ;
for(i = ; i<=m; i++)
if(MAX<dp[i])
MAX = dp[i];
return MAX;
}
int main()
{
int tcase;
scanf("%d",&tcase);
while(tcase--){
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
}
scanf("%d",&m);
for(int i=;i<=m;i++){
scanf("%d",&b[i]);
}
int MAX = LICS();
printf("%d\n",MAX);
if(tcase) printf("\n");
}
return ;
}

hdu 1423(LCS+LIS)的更多相关文章

  1. LCS,LIS,LCIS

    网站:CSUST 8月3日(LCS,LIS,LCIS) LCS:      以下讲解来自:http://blog.csdn.net/yysdsyl/article/details/4226630 [问 ...

  2. HDU 1423 Greatest Common Increasing Subsequence(最长公共上升LCIS)

    HDU 1423 Greatest Common Increasing Subsequence(最长公共上升LCIS) http://acm.hdu.edu.cn/showproblem.php?pi ...

  3. LCS/LIS/LCIS 模板总结

    /************************* LCS/LIS/LCIs模板总结: *************************/ /*************************** ...

  4. 【ACM程序设计】动态规划 第二篇 LCS&LIS问题

    动态规划 P1439 [模板]最长公共子序列 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 题目描述 给出 1,2,-,n 的两个排列 P1 和 P2 ,求它们的最长公共子序列. ...

  5. HDU 1423 LICS 模板

    http://acm.hdu.edu.cn/showproblem.php?pid=1423 4.LICS.O(lena * lenb) 设dp[i][j]表示a[]的前i项,以b[]的第j项结尾时, ...

  6. HDU 1423 最长公共字串+上升子序列

    http://acm.hdu.edu.cn/showproblem.php?pid=1423 在前一道题的基础上多了一次筛选 要选出一个最长的递增数列 lower_bound()函数很好用,二分搜索找 ...

  7. hdu 5495 LCS 水题

    LCS Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5495 Descr ...

  8. HDU - 3564 Another LIS(LIS+线段树)

    http://acm.hdu.edu.cn/showproblem.php?pid=3564 题意 给出1~n的插入顺序,要求每次插入之后的LIS 分析 首先用线段树还原出最终序列.因为插入的顺序是按 ...

  9. LCS,LIS,LCIS学习

    for(int i = 1;i <= n;i++) { int dpmax = 0; for(int j = 1;j <= m;j++) { dp[i][j] = dp[i-1][j]; ...

随机推荐

  1. multi-tap

    multi-tap又称 multi-press . 是在手机,或者电视遥控上的keypad定义,有如下2类标准: 1. ITU-T E.161 2.T9 使用举例如下: Consider a typi ...

  2. LTE 中基于X2的切换

    LTE 中基于X2的切换 (36.300, 23.401)SGW  保持不变 http://blog.sina.com.cn/s/blog_673b30dd0100j4pe.html   1:eNod ...

  3. Java中Model1和Model2

    Model1和Model2是java web的两种架构模式.这两种模式各有优缺点,都有各自适合使用的场景. Model1 首先,从分层的角度说,Model1模式可以看作是由两层组成:视图层和模型层. ...

  4. P1419 寻找段落

    题目描述 给定一个长度为n的序列a_i,定义a[i]为第i个元素的价值.现在需要找出序列中最有价值的“段落”.段落的定义是长度在[S,T]之间的连续序列.最有价值段落是指平均值最大的段落, 段落的平均 ...

  5. [Leetcode] first missing positve 缺失的第一个正数

    Given an unsorted integer array, find the first missing positive integer. For example,Given[1,2,0]re ...

  6. [Leetcode] spiral matrix ii 螺旋矩阵

    Given an integer n, generate a square matrix filled with elements from 1 to n 2 in spiral order. For ...

  7. activity栈清空

    http://blog.csdn.net/swjtuxu/article/details/26163737

  8. 封装安卓的okhttp

    1.封装了get方法,handler更新主线程,回调的onsuccess,onfailure,onerror等方法 2.配置文件 api 'com.android.support:recyclervi ...

  9. 毕业答辩的PPT攻略

    关于内容: 1.一般概括性内容:课题标题.答辩人.课题执行时间.课题指导教师.课题的归属.致谢等.  2.课题研究内容:研究目的.方案设计(流程图).运行过程.研究结果.创新性.应用价值.有关课题延续 ...

  10. tcp/ip网络协议学习

    链路层介绍 网络层协议的数据单元是 IP 数据报 ,而数据链路层的工作就是把网络层交下来的 IP 数据报 封装为 帧(frame)发送到链路上,以及把接收到的帧中的数据取出并上交给网络层. 以太网 以 ...