hdu 1423(LCS+LIS)
题目链接: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)的更多相关文章
- LCS,LIS,LCIS
网站:CSUST 8月3日(LCS,LIS,LCIS) LCS: 以下讲解来自:http://blog.csdn.net/yysdsyl/article/details/4226630 [问 ...
- HDU 1423 Greatest Common Increasing Subsequence(最长公共上升LCIS)
HDU 1423 Greatest Common Increasing Subsequence(最长公共上升LCIS) http://acm.hdu.edu.cn/showproblem.php?pi ...
- LCS/LIS/LCIS 模板总结
/************************* LCS/LIS/LCIs模板总结: *************************/ /*************************** ...
- 【ACM程序设计】动态规划 第二篇 LCS&LIS问题
动态规划 P1439 [模板]最长公共子序列 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 题目描述 给出 1,2,-,n 的两个排列 P1 和 P2 ,求它们的最长公共子序列. ...
- HDU 1423 LICS 模板
http://acm.hdu.edu.cn/showproblem.php?pid=1423 4.LICS.O(lena * lenb) 设dp[i][j]表示a[]的前i项,以b[]的第j项结尾时, ...
- HDU 1423 最长公共字串+上升子序列
http://acm.hdu.edu.cn/showproblem.php?pid=1423 在前一道题的基础上多了一次筛选 要选出一个最长的递增数列 lower_bound()函数很好用,二分搜索找 ...
- hdu 5495 LCS 水题
LCS Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5495 Descr ...
- HDU - 3564 Another LIS(LIS+线段树)
http://acm.hdu.edu.cn/showproblem.php?pid=3564 题意 给出1~n的插入顺序,要求每次插入之后的LIS 分析 首先用线段树还原出最终序列.因为插入的顺序是按 ...
- 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]; ...
随机推荐
- [转]dwr3框架学习笔记--简介及原理简介
1.DWR简介 DWR(直接web远程访问),DWR是一个Java库,使服务器上的Java和JavaScript的浏览器进行交互和相互调用尽可能简单. DWR 是一个可以允许你去创建 AJAX WEB ...
- chromium源码阅读--图片处理
JavaScript 图像替换 JavaScript 图像替换技术检查设备能力,然后“做正确的事”. 您可以通过 window.devicePixelRatio 确定设备像素比,获取屏幕的宽度和高度, ...
- 《学习OpenCV》课后习题解答9
题目:(P126) 创建一个程序,使其读入并显示一幅图像.当用户鼠标点击图像时,获取图像对应像素的颜色值(BGR),并在图像上点击鼠标处用文本将颜色值显示出来. 解答: 本题关键是会用cvGet2D获 ...
- Java中IO——NIO
一.引入 当引入一些新功能的时候,那说明之前的设计可能还需要完善. 1.阻塞式 在传统的IO输入输出中,如果我们从流中去读数据,而数据源中没有数据时,程序就会阻塞该线程.阻塞式线程的一种基本状态,可以 ...
- [C/C++] C++中new的语法规则
int *x = new int; //开辟一个存放整数的存储空间,返回一个指向该存储空间的地址(即指针) ); //开辟一个存放整数的空间,并指定该整数的初值为100,返回一个指向该存储空间的地址 ...
- sqlserver数据库迁移
本篇我们将利用DMA一步一步实现SQL Server 的迁移.帮助大家理解现在的SQL Server与新版本的融合问题,同时需要我们做哪些操作来实现新版本的升级或者迁移. SQL Server 迁移 ...
- c++编辑器下载地址
https://msdn.itellyou.cn/ 输入上述地址选中下图所示的按钮:
- 在linux环境下让java代码生效的步骤
1.kill jboss 2.compile 3.deploy 4.bootstrap jboss.
- bootstrap再次回顾认识到的东西
1,需要使用html5文档类型(Doctype),因此在使用bootstrap项目的开头包含下面的代码段. <!DOCTYPE html> <html> ....... < ...
- C语言编译各过程
1.预处理 此阶段主要完成#符号后面的各项内容到源文件的替换,往往一些莫名其妙的错误都是出现在头文件中的,要在工程中注意积累一些错误知识. (1).#ifdef等内容,完成条件编译内容的替换 (2). ...