LCS,LCIS,LIS模板
来源:互联网 发布:空军一号 国际歌 知乎 编辑:程序博客网 时间:2024/06/04 19:05
LIS#include <cstdio>using namespace std;#define FOR(i, a, n) for(i = a; i <= n; ++i) const int maxn = 5000;int n, a[maxn], f[maxn], i, j, ans;int pre[maxn], path[maxn], pos, len; void setIO() { freopen("in", "r", stdin); freopen("out", "w", stdout);}int main() { setIO(); scanf("%d", &n); FOR(i, 1, n) scanf("%d", &a[i]); FOR(i, 1, n) { f[i] = 1; //初始化 FOR(j, 1, i-1) //增序得出来答案是lower_bound(i)的。。 if(a[j] < a[i] && f[i] < f[j] + 1) { f[i] = f[j] + 1; pre[i] = j; //记录接入的路径 if(ans < f[i]) ans = f[i], pos = i; //记录最长的开始下标 } } printf("%d\n", ans); len = ans; if(ans > 0) path[ans--] = pos; //先加入进去,因为没有记录最后的接入路径 while(ans && pos) { if(pre[pos] > 0) { path[ans--] = pre[pos]; pos = pre[pos]; } } FOR(i, 1, len) printf("%d ", a[path[i]]); return 0;}
LCS#include <cstdio>#include <iostream>using namespace std;#define FOR(i, a, n) for(i = a; i <= n; ++i) void setIO() { freopen("in", "r", stdin); freopen("out", "w", stdout);}const int maxn = 500;int i, j, num, f[maxn][maxn], seq1[maxn], seq2[maxn], sa, sb;int path[maxn]; int max(const int& a, const int& b) { return a < b ? b : a; } int main() { setIO(); scanf("%d%d", &sa, &sb); FOR(i, 1, sa) scanf("%d", &seq1[i]); FOR(i, 1, sb) scanf("%d", &seq2[i]); FOR(i, 1, sa) FOR(j, 1, sb) if(seq1[i] == seq2[j]) f[i][j] = f[i-1][j-1] + 1; else f[i][j] = max(f[i-1][j], f[i][j-1]); for(i = sa, j = sb; j > 0 && i > 0;) {//路径 if(seq1[i] == seq2[j]) { path[num++] = seq1[i]; i--; j--; } else (f[i-1][j] > f[i][j-1]) ? i-- : j--; } cout << f[sa][sb] << endl; for(i = num-1; i >= 0; --i) cout << path[i] << ' '; cout << endl; return 0;}
LCIS#include<cstdio>#include<cstring>int f[1005],a[1005],b[1005],i,j,t,n1,n2,max;int main(){ scanf("%d",&t); while(t--) { scanf("%d%d",&n1,&n2); for(i=1;i<=n1;i++) scanf("%d",&a[i]); for(i=1;i<=n2;i++) scanf("%d",&b[i]); memset(f,0,sizeof(f)); for(i=1;i<=n1;i++) { max=0; for(j=1;j<=n2;j++) { if (a[i]>b[j]&&max<f[j]) max=f[j]; if (a[i]==b[j]) f[j]=max+1; } } max=0; for(i=1;i<=n2;i++) if (max<f[i]) max=f[i]; printf("%d\n",max); }}
阅读全文
0 0
- LCS,LCIS,LIS模板
- LCS/LIS/LCIS 模板总结
- LCS/LIS/LCIS
- LIS LCS LCIS
- LCS LIS LCIS 算法
- LIS && LCS && LCIS
- LCS LIS LCIS学习、
- LCS LIS LCIS 字符串编辑距离 专题
- LCS,LIS等模板
- LIS LCS n^2和nlogn解法 以及LCIS
- ***转载 LIS LCS n^2和nlogn解法 以及LCIS
- LIS LCS n^2和nlogn解法 以及LCIS
- 线性DP总结(LIS,LCS,LCIS,最长子段和)
- LIS LCS LCIS (主要过一遍,重在做题)
- LCS && LIS
- LIS+LCS~
- LCS?LIS
- LIS&&LCS
- linux定时任务的设置 crontab 配置指南
- MyBatis Mapper 接口如何通过JDK动态代理来包装SqlSession 源码分析
- 判断网络
- ng-option
- web service 对外发布一个hello world接口(入门)
- LCS,LCIS,LIS模板
- redis乐观锁实现秒杀
- DrawLayout
- 技术的正宗与野路子
- IT行业offer分析
- 双倍回文
- C++ const总结
- 堆排序
- [kuangbin带你飞]专题十六 KMP & 扩展KMP