题目链接: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. Flink源码解读之状态管理

    一.从何说起 State要能发挥作用,就需要持久化到可靠存储中,flink中持久化的动作就是checkpointing,那么从TM中执行的Task的基类StreamTask的checkpoint逻辑说 ...

  2. 【bzoj2438】[中山市选2011]杀人游戏 Tarjan

    题目描述 一位冷血的杀手潜入 Na-wiat,并假装成平民.警察希望能在 N 个人里面,查出谁是杀手.警察能够对每一个人进行查证,假如查证的对象是平民,他会告诉警察,他认识的人, 谁是杀手, 谁是平民 ...

  3. CSS优化压缩

    顾名思义缩写有简写意思,那就总结一下CSS缩写知识点.为什么要让CSS属性缩写?1.简化代码.一些CSS属性简写可以减少CSS代码从而减少CSS文件的占用字节.加快网页下载速度和网页加载速度.2.优化 ...

  4. LeetCode -- 3SumCloset

    Question: Given an array S of n integers, find three integers in S such that the sum is closest to a ...

  5. 【题解】HAOI2011Problem b

    第一次接触莫比乌斯反演,总之脑子都快要炸掉了……好难啊!本蒟蒻表示根本无法理解呜呜呜呜呜……不过在机房DL的帮助下总算是磕磕绊绊的A掉了这一题: 这道题目要我们的求的是:(1)ΣiΣj [gcd(i, ...

  6. [洛谷P4838]P哥破解密码

    题目大意:求长度为$n$的$01$串中,没有连续至少$3$个$1$的串的个数 题解:令$a_1$为结尾一个$1$的串个数,$a_2$为结尾两个$1$的串的个数,$b$为结尾是$0$的串的个数.$a_1 ...

  7. Hadoop NameNode元数据相关文件目录解析

    在<Hadoop NameNode元数据相关文件目录解析>文章中提到NameNode的$dfs.namenode.name.dir/current/文件夹的几个文件: 1 current/ ...

  8. nginx对指定目录做代理

    环境介绍 web1,作为前端端服务器,访问地址是http://192.168.1.1,要将http://192.168.1.1/bbs的请求交给web2.在web1的网站根目录下并没有bbs目录 we ...

  9. hadoop 架构

  10. Join/remove server into/from windows domain PS script

    Join server into windows domain PS script $username = "ad-domain\admin" $Password = " ...