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]; ...
随机推荐
- 【历史】- Unix时代的开创者Ken Thompson
自图灵奖诞生以来,其获得者一直都是计算机领域的科学家与学者,而在所有这些界的图灵奖中只有唯一的一届有个例外,那就是Ken Thompson与Dennis M. Ritchie,他们都是计算机软件工程师 ...
- div加了float后 四个特性
1.宽度变成0 2.左漂浮 或者右漂浮 3.后面的标签占据原来的位置 4对前面的div没有影响 他会浮动到前面div下面
- Intel QuickAssist Technology and OpenSSL – Benchmarks and Setup Tips
Intel QuickAssist Technology and OpenSSL – Benchmarks and Setup Tips 来源:https://www.servethehome.com ...
- 【题解】[USACO12JAN]视频游戏的连击Video Game Combos
好久没有写博客了,好惭愧啊……虽然这是一道弱题但还是写一下吧. 这道题目的思路应该说是很容易形成:字符串+最大值?自然联想到学过的AC自动机与DP.对于给定的字符串建立出AC自动机,dp状态dp[i] ...
- [Leetcode] Best time to buy and sell stock ii 买卖股票的最佳时机
Say you have an array for which the i th element is the price of a given stock on day i. Design an a ...
- P1641 [SCOI2010]生成字符串
P1641 [SCOI2010]生成字符串 题目描述 lxhgww最近接到了一个生成字符串的任务,任务需要他把n个1和m个0组成字符串,但是任务还要求在组成的字符串中,在任意的前k个字符中,1的个数不 ...
- redux-saga基本用法
redux-saga是管理redux异步操作的中间件,redux-saga通过创建sagas将所有异步操作逻辑收集在一个地方集中处理. sagas采用Generator函数来yield Effects ...
- bulk_insert_buffer_size and InnoDB
Q: I read the following on this page http://dev.mysql.com/doc/mysql/en/server-system-variables.html ...
- [hdu 3068] Manacher算法O(n)最长回文子串
一个不错的讲解:https://github.com/julycoding/The-Art-Of-Programming-By-July/blob/master/ebook/zh/01.05.md # ...
- java注解(Annotation)的简单介绍
注解你可以理解为一个特殊的类,或者接口其自定义个格式形如 public @interface 注解名(){ //注解的属性,特别提醒当注解的属性为value时,在对其赋值时,可以不写value,而直接 ...