LCS/LIS/LCIS 模板总结
/*************************
LCS/LIS/LCIs模板总结:
*************************/ /*****************************************************
LCS:最长公共子序列 求长度为 len1 的序列 A 和长度为 len2 的序列 B 的LCS
注意:序列下标从 0 开始
滚动数组写法。 返回 LCS 长度
*****************************************************/ int LCS(int len1,int len2)
{
memset(dp, 0, sizeof(dp));
for(int i = 1; i <= len1; i++)
{
for(int j = 1; j <= len2; j++)
{
if(s1[i-1] == s2[j-1]) dp[i%2][j] = dp[(i-1)%2][j-1]+1;
else
{
int m1 = dp[(i-1)%2][j];
int m2 = dp[i%2][j-1];
dp[i%2][j] = max(m1, m2);
}
}
} return dp[len1%2][len2];
} /*******************************************
LIS:最长上升子序列 POJ 1257 最少拦截系统
********************************************/
#include<stdio.h>
#include<algorithm>
using namespace std; const int maxn = 1000+10;
const int INF = 30000+10;
int a[maxn]; //导弹高度
int h[maxn]; // h[i] 表示当前第 i 个系统拦截的高度 int main()
{
int n;
while(scanf("%d", &n) != EOF)
{
for(int i = 0; i < n; i++)
{
scanf("%d", &a[i]);
// h[i] = INF;
} h[0] = -1; //保证边界递增
h[1] = a[0]; //第一个
int len = 1; //当前已经确立长度 for(int i = 1; i < n; i++)
{
int index = lower_bound(h,h+len+1,a[i])-h; //保证 h[index] 是数组 h 中第一个 >= a[i] 的
h[index] = a[i];
if(index > len)
len = index;
}
printf("%d\n", len);
}
return 0;
} /***************************************************
LCIS:最长公共上升子序列 求序列 A 长度为 N 和序列 B 长度为 M 的 LCS
序列下标从 1 开始 返回 LCS 长度
*****************************************************/
int dp[maxn];
int LCS(int n, int m)
{
memset(dp, 0, sizeof(dp));
for(int i = 1; i <= n; i++)
{
int tmp = 0; // 存 i 确定, 且 a[i] > b[j] 时最大的 dp[j]
for(int j = 1; j <= m; j++)
{
if(a[i] > b[j] && dp[j] > tmp)
tmp = dp[j];
else if(a[i] == b[j])
dp[j] = tmp+1;
}
} int ans = 0;
for(int i = 1; i <= m; i++)
ans = max(ans, dp[i]);
return ans;
}
LCS/LIS/LCIS 模板总结的更多相关文章
- LCS,LIS,LCIS
网站:CSUST 8月3日(LCS,LIS,LCIS) LCS: 以下讲解来自:http://blog.csdn.net/yysdsyl/article/details/4226630 [问 ...
- 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]; ...
- hdu 1423(LCS+LIS)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1423 好坑啊..还有公共串为0时的特殊判断,还有格式错误..看Discuss看知道除了最后一组测试数据 ...
- 【ACM程序设计】动态规划 第二篇 LCS&LIS问题
动态规划 P1439 [模板]最长公共子序列 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 题目描述 给出 1,2,-,n 的两个排列 P1 和 P2 ,求它们的最长公共子序列. ...
- LCS(记录路径)+LIS+LCIS
https://blog.csdn.net/someone_and_anyone/article/details/81044153 当串1 和 串2 的位置i和位置j匹配成功时, dp[i][j]=d ...
- LCS+LIS
#include<iostream> #include<string> using namespace std; string a,b; ][]; int main() { w ...
- Uva 10635 - Prince and Princess LCS/LIS
两个长度分别为p+1和q+1的由1到n2之前的整数组成的序列,每个序列的元素各不相等,两个序列第一个元素均为1.求两个序列的最长公共子序列 https://uva.onlinejudge.org/in ...
- 最长公共子序列模板(LCS)和LICS模板
递归式: 实例图解: 代码: #include<stdio.h> #include<string.h> ; int dp[N][N],f[N][N]; char a[N],b[ ...
- 【LCS,LIS】最长公共子序列、单调递增最长子序列
单调递增最长子序列 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 求一个字符串的最长递增子序列的长度如:dabdbf最长递增子序列就是abdf,长度为4 输入 ...
随机推荐
- Linux - 进程控制 代码(C)
进程控制 代码(C) 本文地址:http://blog.csdn.net/caroline_wendy 输出进程ID.getpid(). 代码: /*By C.L.Wang * Eclipse CDT ...
- unity, iterate immediate children and iterate all children
遍历所有直接子节点(immediate children): foreach (Transform child in transform) { // do whatever you want with ...
- navigate
一.router.navigate的使用 navigate是Router类的一个方法,主要用来跳转路由. 函数定义: ? 1 navigate(commands: any[], extras?: Na ...
- JUC组件扩展(一):FutureTask理解
一.概述 FutureTask包装器是一种非常便利的机制,同时实现了Future和Runnable接口. 类图如下: FutureTask是一种可以取消的异步的计算任务.它的计算是通过Callable ...
- Atitit. Class 元数据的反射操作 api apache 工具
Atitit. Class 元数据的反射操作 api apache 工具 1 BeanUtils & PropertyUtils & MethodUtils类使用方法 - 短裤党 ...
- [elk]logstash的最佳实战-项目实战
重点参考: http://blog.csdn.net/qq1032355091/article/details/52953837 不得不说这是一个伟大的项目实战,是正式踏入logstash门槛的捷径 ...
- 实例探索Class文件
class文件是指以.class为文件后缀的Java虚拟机可装载文件.无论该class文件是在linux上进行编译的,还是在windows环境下编译的,无论虚拟机是在何种平台下实现和运行的,class ...
- Android Screen Monitor
实现屏幕同步前提是安装有JDK和配置好ADB的环境变量 1.官方地址 http://code.google.com/p/android-screen-monitor/ 2.解压缩得到asm.jar 3 ...
- c++的.o文件的链接顺序
linker对链接顺序要求很严格,如果顺序有误,多半就会报undefined reference to xxxxxx的错误 文件目录: 代码: main.cpp #include "Test ...
- RxBinding -- 官网说明
RxBinding -- 官网说明 新建 模板 小书匠 作用 组件 平台绑定 support-v4 绑定 appcompact-v7 绑定 design 库绑定 recyclerview-v7 绑定 ...